[英]Postgres SQL - Concatenate Tables with different columns
I've looked up ways to solve my problem here on SO and other sources, but nothing i have tried worked, so here i am.我已经在 SO 和其他来源上查找了解决我的问题的方法,但是我尝试过的没有任何工作,所以我在这里。
I need to concatenate two tables with different columns, as it follows:我需要连接两个具有不同列的表,如下所示:
(data is just a represetantion, not my actual data and i'm using Postgres SQL) (数据只是一个代表,不是我的实际数据,我正在使用 Postgres SQL)
Table_1:表格1:
name price id location
test 1.0 7 Canada
Table_2表_2
name store sale price location
testing local_store 54 2.0 US
My actual tables have over 30 rows each, but the resulting table i need would look like this:我的实际表每个有超过 30 行,但我需要的结果表如下所示:
Table_concat:表连接:
name price store sale id location
test 1.0 null null 7 Canada (row from Table_1)
testing 2.0 local_store 54 null US (row from Table_2)
Basically, i need to put one table on top of another and when the columns do not match, a null value should apear.基本上,我需要将一张桌子放在另一张桌子上,当列不匹配时,应该出现 null 值。 Can anyone help me?
谁能帮我? I can provide further explanation if necessary.
如有必要,我可以提供进一步的解释。
Try this query:试试这个查询:
SELECT
name, price, null AS store, null AS sale, id, location
FROM Table_1
UNION
SELECT
name, price, store, sale, null AS id, location
FROM Table_2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.