我有这些表
table 1
id price
1 30
2 40
3 50
table 2
id price
1 70
2 5
3 10
我想要一个基于price
汇总ID
值的查询
比如table1.id = table2.id然后总和table1.price和table2.price
最终结果应该是这样的
table 3
id price
1 100
2 45
3 60
答案 0 :(得分:2)
你可以;
SELECT
TABLE1.ID,
TABLE1.PRICE+TABLE2.PRICE
FROM TABLE1
INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.ID;
或者如果两个表中都有重复的ID;
SELECT
TABLE1.ID,
SUM(TABLE1.PRICE+TABLE2.PRICE)
FROM TABLE1
INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.ID
GROUP BY TABLE1.ID;