发布网友
共4个回答
热心网友
select A.a,B.b, sum(A.a+B.b) from A, B where A.n=B.n group by A.n,A.a,B.b;
追问您的这条语句只能选择出,在A表和B表都存在n字段的情况下,我想要的是,不管存不存在n,都要有数据输出,如果,a字段不存在则总和为b字段的值,如果b字段不存在则总和为a字段的值
,呵呵
追答select A.a A,B.b B,sum(A.a+B.b) N from A,B where A.n=B.n group by A.n,A.a,B.b
union
select a,0,n from A where A.id not in (select A.id from A,B where A.n=B.n )
union
select 0,b,n from B where B.id not in (select B.id from A,B where A.n=B.n )
ORDER BY N DESC
;
热心网友
select A.a,B.b, sum(A.a+B.b) from A inner join B on A.n=B.n group by A.n
热心网友
select A.a,B.b,SUM(A.a+B.b)as c from A inner join B on A.n=B.n
热心网友
select sum(table.a)
from
(
select id,a,n from A
union all
select id,b as a,n from B
) table group by table.n order by table.n