标签:blog http io sp div 2014 on log bs
有两张表
a b
每张表里有两个字段都是id num
a表的值
id num b表的值 id num
a 5 b 15
b 10 c 20
c 15 d 20
d 20 e 30
求出两张表的结果 用一条sql
结果值
id num
a 5
b 25
c 35
d 40
e 30
SELECT `id`, SUM(`num`) FROM (SELECT * FROM `b` UNION ALL SELECT * FROM `a` ) as new_table GROUP BY `id`
select * from b UNION ALL select * from a 先合并两张表,求并集,a,b两张表的select columns 要相同
然后通过GROUP BY 分组求和
标签:blog http io sp div 2014 on log bs
原文地址:http://www.cnblogs.com/qifeng1991/p/4025608.html