a表
id   a1   a2  a3
1    11   12   13
2    21   22   23
3    31   32   33
b表
id   b1   b2   b3
1    1    2     3
2    4    5     6
3    7    8     9
查询结果
id  a1  a2  b1  b3
1   11  12  1   3
2   21  22  4   6
3   31  32  7   9
合计        12  18 
sql语句如何写,谢谢!

解决方案 »

  1.   

    select id=ltrim(a.id),a1=ltrim(a1),a2=ltrim(a2),b1,b2 from a,b where a.id=b.id
    union all
    select '合计',null,null,sum(b1),sum(b2) from b
      

  2.   

    create table a
    (aid int, a1 int,a2 int ,a3 int
    )
    create table b
    (bid int, b1 int,b2 int ,b3 int
    )
    insert a
    select 1,11,12,13 union
    select 2,21,22,23 union
    select 3,31,32,33
    insert b
    select 1,1,2,3 union
    select 2,4,5,6 union
    select 3,7,8,9
    select a.aid,a.a1,a.a2,b.b1,b.b3 from a as a join b as b on a.aid=b.bid
    compute sum(b.b1) ,sum(b.b3)