本文介绍了在PostgreSQL中添加两个关系的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个关系,例如Relation1和Relation2。关系1的列为A,B,C,关系2的列为D,E,F。
I have two relations such as relation1 and relation2. relation1 has columns of A,B,C and relation2 has columns of D,E,F.
我想将关系1的A与关系2的D相加,其中C = F对于在lation2中不存在的C值必须出现而在relation1中不出现的F值也必须出现。
I want to add A of relation1 with D of relation2 where C = F. For the C values which do not exist in relation2 must appear and F values which do not appear in relation1 also must appear How to do this postgresql?
推荐答案
使用可以包含任一侧的行,而另一侧不包含匹配行:
Use a FULL [OUTER] JOIN
to include rows from either side without a matching row on the other side:
SELECT COALESCE(r1.a, 0) + COALESCE(r2.d, 0) AS a_d
FROM relation1 r1
FULL JOIN relation2 r2 ON r1.c = r2.f
也可以使用即可 NULL
值替换缺少的列。
Also use COALESCE()
to catch NULL
values substituted for missing columns.
这篇关于在PostgreSQL中添加两个关系的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!