在MySQL中汇总UNION结果的一种简单方法 [英] a simple way to sum a result from UNION in MySql
本文介绍了在MySQL中汇总UNION结果的一种简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个表(t1,t2,t3)的并集. 每个重新运行的记录数完全相同,第一列是id,第二个量是:
I have a union of three tables (t1,t2,t3). Each rerun exactly the same number of records, first column is id, second amount:
1 10
2 20
3 20
1 30
2 30
3 10
1 20
2 40
3 50
是否有简单的sql方式将其总结为:
Is there a simple in sql way to sum it up to only get:
1 60
2 80
3 80
推荐答案
select id, sum(amount) from (
select id,amount from table_1 union all
select id,amount from table_2 union all
select id,amount from table_3
) x group by id
这篇关于在MySQL中汇总UNION结果的一种简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文