b表:要select cid,sum(count) from b where cid=1 group by cid;

a表: 要select cid,sum(amount) from a where cid=1 group by cid;
需要将两条select统计值一起展示,解决如下:
--缺少对应列这里为了统计用0代替(数据类型要要致),
select cid,sum(a),sum(b)
from (SELECT cid, a.amount a, 0 'b' from a where a.cid = 1
union
select cid, 0, b.`count` b from b where b.cid = 1) c
group by c.cid;
本文介绍了一个具体的SQL查询案例,展示了如何使用联合查询将两个不同表中的数据进行汇总统计,并通过填充0来处理缺失的列值,确保数据类型的统一。

被折叠的 条评论
为什么被折叠?



