[英]Adding columns of two relations in postgresql
I have two relations such as relation1 and relation2. 我有两个关系,如relation1和relation2。 relation1 has columns of A,B,C and relation2 has columns of D,E,F.
关系1的列为A,B,C,关系2的列为D,E,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? 我想将relation1的A与relation2的D相加,其中C =F。对于relation2中不存在的C值必须出现,而在relation1中不出现的F值也必须出现。如何执行此PostgreSQL?
Use a FULL [OUTER] JOIN
to include rows from either side without a matching row on the other side: 使用
FULL [OUTER] JOIN
从任一侧包含行,而在另一侧不包含匹配行:
SELECT COALESCE(r1.a, 0) + COALESCE(r2.d, 0) AS a_d
FROM relation1 r1
FULL JOIN relation2 r2 ON r1.c = r2.f
Also use COALESCE()
to catch NULL
values substituted for missing columns. 还可以使用
COALESCE()
来捕获替换缺失列的NULL
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.