在Postgresql中连接两个表,同时保持不匹配的行

时间:2018-06-14 20:35:56

标签: postgresql join sql-update postgis

我正在尝试使用PostGIS扩展程序在Postgresql中使用非空间表连接空间表。

spatial_table:

geom   | attribute1 | Key |
foobar | foobar     |  1  |
foobar | foobar     |  2  |
foobar | foobar     |  3  |
foobar | foobar     |  4  |

non_spatial_table:

attribute2 | attribute3 | Key |
foobar     | foobar     |  1  |
foobar     | foobar     |  4  |

joined_table:

geom   | attribute1 | Key | attribute2 | attribute3 |
foobar | foobar     |  1  | foobar     | foobar     |
foobar | foobar     |  2  | NULL       | NULL       |
foobar | foobar     |  3  | NULL       | NULL       |
foobar | foobar     |  4  | foobar     | foobar     |

按NULL我的意思是空的。

以下代码有效:

CREATE TABLE joined_table AS
SELECT *
FROM spatial_table
JOIN non_spatial_table ON spatial_table.title_no = non_spatial_table.title_number;

但是,spatial_table中不等于non_spatial_table的所有行都不在结果表中。

结果表:

geom   | attribute1 | Key | attribute2 | attribute3 |
foobar | foobar     |  1  | foobar     | foobar     |
foobar | foobar     |  4  | foobar     | foobar     | 

我也尝试过:

ALTER TABLE spatial_table
    ADD COLUMN title_number varchar,
    ADD COLUMN tenure varchar
UPDATE spatial_table
    SET title_number = non_spatial_table.title_number
FROM spatial_table INNER JOIN non_spatial_table ON spatial_table.title_no = non_spatial_table.title_number

但是我收到以下错误:

ERROR: table name "spatial_table" specified more than once SQL state: 42712

有谁知道如何实现这种类型的加入?

2 个答案:

答案 0 :(得分:2)

你想要一个LEFT OUTER JOIN。我用varchar字段模拟了这个例子(空间部分在这里并不重要),加入了关键字段。

postgres@sandbox=# select * from spatial_table;
 key |  geom   | attribute1
-----+---------+------------
   1 | foobar1 | foobar1
   2 | foobar2 | foobar2
   3 | foobar3 | foobar3
   4 | foobar4 | foobar4
(4 rows)

postgres@sandbox=# select * from non_spatial_table;
 key | attribute2 | attribute3
-----+------------+------------
   1 | foobar12   | foobar13
   4 | foobar42   | foobar43
(2 rows)

postgres@sandbox=# select a.geom, a.attribute1, a.key, b.attribute2, b.attribute3
sandbox-# from spatial_table a
sandbox-# left outer join non_spatial_table b
sandbox-# on a.key=b.key;
  geom   | attribute1 | key | attribute2 | attribute3
---------+------------+-----+------------+------------
 foobar  | foobar1    |   1 | foobar12   | foobar13
 foobar2 | foobar2    |   2 | [NULL]     | [NULL]
 foobar3 | foobar3    |   3 | [NULL]     | [NULL]
 foobar4 | foobar4    |   4 | foobar42   | foobar43
(4 rows)

只需在CTAS中使用最后一个查询(可能视图或物化视图会更有意义)。

答案 1 :(得分:1)

我尝试过Don Seilers解决方案,如下所示:

SELECT a.geom, a.title_no, b.title_number, b.tenure
FROM spatial_table a
LEFT OUTER JOIN non_spatial_table b
ON a.title_no = b.title_number;

它运行,但随后耗尽内存。我增加了postgresql.conf中的work_mem和共享缓冲区没有效果。有没有办法让上面的代码更有效率,或者我错过了postgresql.conf设置的东西?

更新:上面的代码有效,内存问题被证明是一个完全不同的问题。

相关问题