使用具有相同主键的第三个 table 连接两个具有公共字段的 table

Joining two tables with common fields by using a third table with same primary key

我正在尝试在 3 tables 2 of the table share mostly of the fields (just a couple of them are different) 我想知道我怎么能创建此连接,将公共字段合并到一列中,留下 null/blank 那些在不存在数据时不常见的字段。

table 1:

PK1 geom field 2 field 3 field 4 field 5
1    1-1     1-2     1-3     1-4     1-5    
2    2-1     2-2     2-3     2-4     2-5   
3    3-1     3-2     3-3     3-4     3-5   
4    4-1     4-2     4-3     4-4     4-5   
5    5-1     5-2     5-3     5-4     5-5   

table a:

    PK1 field 1ab field 2ab field 3ab field 4ab field 5a
    1    1-1a       1-2a         1-3a     1-4a     1-5a     
    2    2-1a       2-2a         2-3a     2-4a     2-5a     
    3    3-1a       3-2a         3-3a     3-4a     3-5a      
    4    4-1a       4-2a         4-3a     4-4a     4-5a      
    5    5-1a       5-2a         5-3a     5-4a     5-5a
  

table b:

    PK1 field 1ab field 2ab field 3ab field 4ab field 5b field 6b
    6    1-1b       1-2b      1-3b     1-4b        1-5b      1-6b
    7    2-1b       2-2b      2-3b     2-4b        2-5b      2-6b
    8    3-1b       3-2b      3-3b     3-4b        3-5b      3-6b
    9    4-1b       4-2b      4-3b     4-4b        4-5b      4-6b
    10   5-1b       5-2b      5-3b     5-4b        5-5b      5-6b

结果 TABLE 加入 TABLE 1 个 PK:

PK1  geom    field 2ab field 3ab field 4ab field 5a field 5b field 6b
1    1-1       1-2a      1-3a      1-4a      1-5a     null     null
2    2-1       1-2a      1-3a      1-4a      1-5a     null     null 
3    3-1       1-2a      1-3a      1-4a      1-5a     null     null
4    4-1       1-2a      1-3a      1-4a      1-5a     null     null
5    5-1       1-2b      1-3b      1-4b      null     null     null
6    6-1       1-2b      1-3b      1-4b      null     1-5b     1-6b
7    7-1       1-2b      1-3b      1-4b      null     1-5b     1-6b
8    8-1       1-2b      1-3b      1-4b      null     1-5b     1-6b
9    9-11       1-2b      1-3b      1-4b      null     1-5b     1-6b

我匹配得到所有带内部连接的列,但我无法得到我想要的结果,关于如何获得这个的任何想法?我在这里检查过,但找不到任何答案,我也尝试过聚结,但没有得到我期待的结果。

这是我获取每个 table 的所有字段的查询类型,因此很多字段重复。

SELECT t1.pk,
           t2.field 1ab,
        t2.field 1ab... t3.field 5b...
       FROM table 1 t1
         LEFT JOIN table b t2 ON t1.pk::text = t2.pk::text
         LEFT JOIN ocod t3 ON t3.pk::text = t1.pk::text
      GROUP BY
    t1.pk,
           t2.field 1ab,
        t2.field 1ab...
     

我在这方面学到了一些东西。在您的示例中,我看不到 table t1 与期望结果的关系。期望的结果似乎只是 t2 和 t3 的组合。我从这个 Answer which uses a NATURAL JOIN.

得到了解决方案的内核

首先创建 tables:

BEGIN;
CREATE TABLE t1 (
    PK1     text,
    field_1 text,
    field_2 text,
    field_3 text,
    field_4 text,
    field_5 text
);

INSERT INTO t1 VALUES
('1', '1-1', '1-2', '1-3', '1-4', '1-5'), 
('2', '2-1', '2-2', '2-3', '2-4', '2-5'),
('3', '3-1', '3-2', '3-3', '3-4', '3-5'), 
('4', '4-1', '4-2', '4-3', '4-4', '4-5'),
('5', '5-1', '5-2', '5-3', '5-4', '5-5');

CREATE TABLE t2 (
       PK1       text,
    field_1ab text,
    field_2ab text,
    field_3ab text,
    field_4ab text,
    field_5a  text
);

INSERT INTO t2 VALUES
('1',  '1-1a',  '1-2a',  '1-3a',  '1-4a',  '1-5a'), 
('2',  '2-1a',  '2-2a',  '2-3a',  '2-4a',  '2-5a'),
('3',  '3-1a',  '3-2a',  '3-3a',  '3-4a',  '3-5a'), 
('4',  '4-1a',  '4-2a',  '4-3a',  '4-4a',  '4-5a'),
('5',  '5-1a',  '5-2a',  '5-3a',  '5-4a',  '5-5a');

CREATE TABLE t3 (
    PK1       text,
    field_1ab text,
    field_2ab text,
    field_3ab text,
    field_4ab text,
    field_5b  text,
    field_6b  text
);

INSERT INTO t3 VALUES
('6', '1-1b', '1-2b', '1-3b', '1-4b', '1-5b', '1-6b'), 
('7', '2-1b', '2-2b', '2-3b', '2-4b', '2-5b', '2-6b'),
('8', '3-1b', '3-2b', '3-3b', '3-4b', '3-5b', '3-6b'), 
('9', '4-1b', '4-2b', '4-3b', '4-4b', '4-5b', '4-6b'),
('10', '5-1b', '5-2b', '5-3b', '5-4b', '5-5b', '5-6b');

COMMIT;

现在开始查询。看起来如果你做一个 NATURAL FULL OUTER JOIN,PostgreSQL 会尝试合并尽可能多的列。

  SELECT *
    FROM (t2 NATURAL FULL OUTER JOIN t3)
ORDER BY pk1::int

这给出了

pk1 field_1ab field_2ab field_3ab field_4ab field_5a field_5b field_5b
1 1-1a 1-2a 1-3a 1-4a 1-5a [NULL] [NULL]
2 2-1a 2-2a 2-3a 2-4a 2-5a [NULL] [NULL]
3 3-1a 3-2a 3-3a 3-4a 3-5a [NULL] [NULL]
4 4-1a 4-2a 4-3a 4-4a 4-5a [NULL] [NULL]
5 5-1a 5-2a 5-3a 5-4a 5-5a [NULL] [NULL]
6 1-1b 1-2b 1-3b 1-4b [NULL] 1-5b 1-6b
7 2-1b 2-2b 2-3b 2-4b [NULL] 2-5b 2-6b
8 3-1b 3-2b 3-3b 3-4b [NULL] 3-5b 3-6b
9 4-1b 4-2b 4-3b 4-4b [NULL] 4-5b 4-6b
10 5-1b 5-2b 5-3b 5-4b [NULL] 5-5b 5-6b