合并和添加两个表中的值 [英] Merge and add values from two tables

查看:95
本文介绍了合并和添加两个表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以设计一个在两个表中添加值的查询:

Is it possible to craft a query that adds values within two tables:

例如,假设您有两个表

id    value
--    -----
a       1
c       2
d       3
f       4
g       5

id     value
--     -----
a        1
b        2
c        3
d        4
e        5

然后,当您添加两个表时,您将获得ID匹配的结果。因此,a = 1 + 1 = 2,结果却相同。因此查询将返回:

Then when you 'add' the two tables you would get the result where the id's match. So, a=1+1=2, and simply the same result where they don't. So the query would return:

id     value
--     -----
a       2
b       2
c       5
d       7
e       5
f       4
g       5


推荐答案

可能类似于

select coalesce(t1.id, t2.id) as id, (coalesce(t1.value, 0) + coalesce(t2.value, 0)) as value 
from table1 t1 full outer join table2 t2 on t1.id = t2.id

这篇关于合并和添加两个表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆