三个表结构如下
a表   aid  name
      1    a
      2    bb表   bid    aid
       1     1
       2     2
       3     2c表   cid     bid
      1       1
      2       2
      3       2
      4       3
      5       3求一个Sql语句  得出a表关联b表和c表的记录总数
      aid    name    bcount    ccount
       1      a       1          1
       2      b       2          4

解决方案 »

  1.   

    select aid, name, count(*)bcount, sum(num) ccount
    from 
    (
        select a.aid aid, a.name name, bid bid
               ,(select count(*) from c where c.bid=b.cid) num
        from a, b 
        where a.bid=b.bid
    )
    group by aid, name
    这样应该可以实现你要的功能(没有测试,可能会有点小问题)
    就是不知道效率怎么样了
      

  2.   

    结果出来了  参考yifanernei(f)的提示  正解应该是
    select t.aid,t.name , count(t.bid) as bcount , sum(t.num) as ccount
    from
    (select a.aid , a.name, b.bid,(select count(c.bid) from c where c.bid=b.bid) as num
        from a, b
        where a.aid=b.aid) as t
    group by t.aid
    感谢yifanernei(f)的提示  虽然yifanernei(f)的提示有点小问题  可能效率也不高  不过还是要感谢yifanernei(f)