biaoA
id      name        aa         bb       cc   .........
--------------------------------------------------
631     zhang       fdfa       53645    oip   ........
388      li         fda        986967   ioio  .........
1085     li         hgfg       7657     jkfj  .......
689     chen        fdsafds    563634   jhfgjf ......biaoB
Bid    Bname    score  other
----------------------------
631    zhang    79     xxxx 
631    zhang    53     yyyy
388    li       90     zzzz
631    zhang    60     gggggg
689    chen     70     gfdggd
1085   li       99     fdasfas
388    li       45     afdda
1085   li       69     fadfdas我想取出所有biaoA的数据,外加对应biaoB的sum(biaoB.score)
即得到类似如下表
id   name    aa      bb      cc   ........... totalscore
-------------------------------------------------------------
631   zhang   fdfa     53645    oip   ........    192
388    li     fda      986967   ioio  .........   135
1085   li     hgfg     7657     jkfj  .......     168
689   chen    fdsafd   563634   jhfgjf ......     70现在我只会如下的sql查询
select   aa,
         bb,
         cc,
         sum(score) as totalscore,
         name,
         id 
from biaoA,biaoB 
where id=Bid and name=Bname
group by id,name,aa,bb,ccbiaoA中列一多起来,我的实现就太麻烦了,我觉得用group by 和 join应该可以
很方便的实现类似于
select * from biaoA join biaoB on id=Bid and name=Bname group by id
当然,我这个实现是错误的,提示biaoA的其他列未包含在group by中请教应该如何实现,万分感谢。

解决方案 »

  1.   

    select a.*,
    totalsocre=SUM(score)
    from a join b on a.bid=b.bid
    group by id   ,   name ,      aa   ,     bb   ,   cc  ......... 
      

  2.   

    select a.*,
    totalsocre=SUM(score)
    from a join b on a.bid=b.bid
    group by a.id   ,   a.name ,      a.aa   ,     a.bb   ,   a.cc  ......... 
    --后面Group by a的所有列
      

  3.   


    select biaoA.*,(select sum(score) from biaoB  where Bid=A.id and Bname=A.name ) from biaoA A
      

  4.   

    SELECT AA,BB,CC,ISNULL(TOTALSCORE,0)
    FROM BIAOA A
    LEFT JOIN (
    SELECT BID,SUM(SCORE) 'TOTALSCORE'
    ) B ON A.ID=B.BID
      

  5.   


    select A.*,(select sum(score) from biaoB  where Bid=A.id and Bname=A.name ) from biaoA   A
      

  6.   

    select a.*,p
    totalsocre=SUM(score)
    from a join (select id ,SUM(score) as p from b  group by id) k on a.bid=k.bid
      

  7.   

    select A.*,(select sum(score) from biaoB  where Bid=A.id and Bname=A.name )as TOTALSCORE from biaoA   A
      

  8.   

    select a.*,totalsum 
    from a join (select bid,SUM(score) as totalsum from b  group by bid) k on a.bid=k.bid
      

  9.   

    感谢回复因为biaoA中的列十分的多,不想一一group by出来,主要是这个问题。
    是否通过其他方法实现只group by id ?
      

  10.   

    select a.*,b.score
    from biaoA
        join (
            select bid,sum(score) score
            from biaoB  
            group by bid
        ) b
          on a.id=b.bid
      

  11.   

    --照这个样子改吧。
    select  a.id,a.name,a.aa,a.bb,a.cc,totalscore=(select sum(score) from biaoB where bid=b.bid)
    from biaoA a 
    left join biaoB  b
    on a.id=b.Bid and a.name=b.Bname 
      

  12.   

    --写最后一次
    select a.*,totalscore 
    from a join (select bid,SUM(score) as totalscore from b  group by bid) k on a.id=k.bid
      

  13.   

    select a.*,b.score
    from biaoA as a --漏了个别名
        join (
            select bid,sum(score) score
            from biaoB  
            group by bid
        ) as b
          on a.id=b.bid
      

  14.   


    ----试
    select A.*,
           (select sum(score) from biaoB  where Bid=A.id and Bname=A.name ) as otalscore 
    from biaoA   A
      

  15.   

    --干脆这样得了。
    select  a.*,totalscore=(select sum(score) from biaoB where bid=a.id)
    from biaoA a 
      

  16.   

    用group by的时候,选择列表中字段必须全部存在于group by 中
      

  17.   

    我的答案在4楼,和T-MAC在13楼写的一样。。
    呵呵
      

  18.   

    细细品味学习各楼不同答案ing...3楼的大大就已经基本成功了,so 感谢40分,剩下的大家平分了。10多楼的回复,学到很多东西,再次感谢各位热心回复。