我有2个表,结构为(id,views)
Table 1:
id views
A 1
B 2
B 3
C 3
C 4
D 4
Table 2:
id views
C 1
D 3
D 4
E 5
E 7
F 8
我希望在这种情况下对表1和表2中的ID(id C和D)进行求和,这样输出就是:
Table 3:
id views
C 8
D 11
答案 0 :(得分:0)
您可以在案例中使用以下查询:
select a.id,sum(a.views) from ( select * from table1 union table2 ) as a group by id;
答案 1 :(得分:0)
选择id,sum(views)from(select * from table1 union all select * from table2)a其中a.id =“C”或a.id =“D”group by id;