sql 查询结果相加

SELECT count(*) FROM sale_1110 where salerid ="01022";
SELECT count(*) FROM sale_1109 where salerid ="01022";
SELECT count(*) FROM sale_1108 where salerid ="01022";
SELECT count(*) FROM sale_1107 where salerid ="01022";
如将这几结果相加

sql查询结果相加。例子:select a.snum1 + b.snum2 from。(select sum(num1) snum1 from table1)a ,(select sum(num2) snum2 from table2)b。这样就能把a表和b表的结果相加看。(select sum(num1) snum1 from table1)a 用于统计table1表中num1字段的总和 其结果作为表a。(select sum(num2) snum2 from table2)b 用于于统计table2表中num2字段的总和 其结果作为表a。select a.snum1 + b.snum2 将a表和b表中的sum1和sum2相加。这样既可做查询结果相加。
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2018-02-10
sql查询结果相加
例子:
select a.snum1 + b.snum2 from
(select sum(num1) snum1 from table1)a ,
(select sum(num2) snum2 from table2)b
这样就能把a表和b表的结果相加看
(select sum(num1) snum1 from table1)a 用于统计table1表中num1字段的总和 其结果作为表a
(select sum(num2) snum2 from table2)b 用于于统计table2表中num2字段的总和 其结果作为表a
select a.snum1 + b.snum2 将a表和b表中的sum1和sum2相加
这样既可做查询结果相加本回答被网友采纳
第2个回答  推荐于2016-09-01
select sum(col) from (
SELECT count(*) col FROM sale_1110 where salerid ="01022" union all
SELECT count(*) col FROM sale_1109 where salerid ="01022" union all
SELECT count(*) col FROM sale_1108 where salerid ="01022" union all
SELECT count(*) col FROM sale_1107 where salerid ="01022")追问

select sum(col) from (
SELECT count(*) col FROM sale_1110 where salerid ="01022" union all
SELECT count(*) col FROM sale_1109 where salerid ="01022和select sum(col) from (
SELECT count(*) col FROM sale_1110 where salerid ="01021" union all
SELECT count(*) col FROM sale_1109 where salerid ="01021" union all
可以整编成,结果是二列

追答

??

追问

条件salerid 不一样,不想一个个查,可以一个语句查出来吗? 当只是salerid一样的求和

追答

如果数据量大,建议使用索引.

select count(salerid ) from (
SELECT salerid FROM sale_1110 union all
SELECT salerid FROM sale_1109 union all
SELECT salerid FROM sale_1108 union all
SELECT salerid FROM sale_1107) where salerid ="01022"

本回答被提问者采纳
第3个回答  2012-01-12
select qty=sum(qty) from
(
SELECT qty=count(*) FROM sale_1110 where salerid ="01022"
Union All
SELECT qty=count(*) FROM sale_1109 where salerid ="01022"
Union All
SELECT qty=count(*) FROM sale_1108 where salerid ="01022"
Union All
SELECT qty= count(*) FROM sale_1107 where salerid ="01022"
) a
相似回答