我想做如下操作
select table1.a,sum(table2.b)
from table1,table2
where.... 
但是是无法完成的,我知道错误原因,想知道这样的操作,即 从一个表里去一个值,从另一个表里去求和的值,如何来完成,我需要同时

解决方案 »

  1.   

    最后加上
    Groupby table1.a领Form,中两个表最好使用JOIN一楼
      

  2.   

    建议楼主更正SQL书写的风格!
    select a.a, sum(b.b)
    from table1 a, table2 b
    where ...
    group by a.a
      

  3.   

    分两种情况讨论:
    一、在Delphi中实现
      1、先用一个adodataset把sum(*)求出来,假设赋值给 nSum    //其中的where 与下面的Where设为一样
      2、var
            strSql:String;
         Begin
            strSql:='Select table.a, ' + nsum + ' from ...'  //Where
            adodataset.close;
            adodataset.commandtext:=strSql;
            adodataset.open;
         End;二、在SQL 查询分析器中实现
        Declare @nSum float
        Select @nSum=sum(*) from ...     //where  
        Select Table1.a,@nSum from ...   //where 两个where 设为一样    保证能通过,我是个老实人,别忘了给我…… ^_^
      

  4.   

    但如果是如下情况我该如何GROUP BY
    select a1.a+a2.b,sum(a3.c)
    from ...
    where...
      

  5.   

    jozosoft(水灵灵的乌兰诺娃):你的方法的确能行,但太累了,我取的sum太多了,而且这样凑sql语句太累了,我是做 insert into table select()这样的操作~~~
      

  6.   

    select a.a, sum(b.b)
    from table1 a, table2 b
    where ...关联table1.c=table2.c
    group by a.a
      

  7.   

    但如果是如下情况我该如何GROUP BY
    select a1.a+a2.b,sum(a3.c)
    from ...
    where...----------------
    一样的,group by a1.a+a2.b
      

  8.   

    如果使用sum,max等统计函数,那么没有包含这些函数的列要写在group by 中