我有两个表
table1: a ,b
table2:c
       1
       2
.....
我要实现:
select sum(a),sum(b) from table1 where 按table2:c中的字段,分别做汇总,C中有很多条记录
例如:
select sum(a),sum(b) from table1 where x=table2.c中的1
select sum(a),sum(b) from table1 where x=table2.c中的2
.....

解决方案 »

  1.   

    --示意代码
    SELECT 
       'SUM1'=(SELECT SUM(A) FROM TABLE1 WHERE A.C=X ),
    FROM TABLE2 A
    具体可参见相关子查询概念
      

  2.   

    select sum(a),sum(b) from table1 where x=table2.c中的1上面的x是什么?没有这个字段啊!
      

  3.   

    x. 是条件;
    有没办法循环得到table2中每一条记录的值啊,table2中只有一个字段
    然后我在用这个值分别去汇总
      

  4.   

    X当然是与table1相对应的ID列了!
      

  5.   

    select sum(a),sum(b) from table1 where x in (select 1 from table2);
    select sum(a),sum(b) from table1 where x in (select 2 from table2)不知道是不是LZ要的意思