Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 合并多个表_Sql_Postgresql_Full Outer Join - Fatal编程技术网

Sql 合并多个表

Sql 合并多个表,sql,postgresql,full-outer-join,Sql,Postgresql,Full Outer Join,假设有以下三个PostgreSQL表要与公共ID列合并 table A ID V1 2 100 3 200 4 250 table B ID V2 1 200 3 140 table C ID V3 2 90 3 100 4 10 5 200 我想将这三个表合并如下: merged table ID V1 V2 V3 1 200 2 100 90 3 200 140 100 4 250 10 5

假设有以下三个PostgreSQL表要与公共ID列合并

table A  
ID V1
2  100
3  200
4  250

table B
ID V2
1  200
3  140

table C
ID V3
2  90
3  100
4  10
5  200
我想将这三个表合并如下:

merged table
ID V1   V2   V3
1       200  
2  100       90
3  200  140  100
4  250       10
5            200

我感谢你的帮助

由于您只希望id列在结果中出现一次,因此明显的选择是使用
USING
子句进行对等连接:

    select A.*,B.V2,C.V3
    from 
    A 
    full outer join B on A.id = B.id
    full outer join C on C.id = coalesce(A.id,B.id)
SELECT id, v1, v2, v3
FROM   a 
FULL OUTER JOIN b USING (id)
FULL OUTER JOIN c USING (id)
通过这种方式,您根本不需要
合并
,这会很快让很多表变得混乱


注意-我还没有测试过这个。它的工作原理不太一样。对于
id
,它需要
COALESCE()