2
假设我有以下表格:如何查询树结果的Postgres设置包括多个表
drop table if exists city;
drop table if exists country;
drop table if exists world;
create table world (
id integer PRIMARY KEY,
name text not null,
population bigint not null
)
;
create table country (
id integer PRIMARY KEY,
world_id integer REFERENCES world (id),
name text not null,
population bigint not null
);
create table city (
id integer PRIMARY KEY,
country_id integer REFERENCES country (id),
name text not null,
population bigint not null
);
insert into world (id, name, population) values (1, 'World', 7125000000);
insert into country (id, world_id, name, population) values (2, 1, 'Austria', 8000000);
insert into country (id, world_id, name, population) values (3, 1, 'Poland', 38530000);
insert into city (id, country_id, name, population) values (4, 2, 'Vienna', 1741000);
insert into city (id, country_id, name, population) values (5, 2, 'Salzburg', 145000);
insert into city (id, country_id, name, population) values (6, 3, 'Warsaw', 1710000);
insert into city (id, country_id, name, population) values (7, 3, 'Stetin', 409000);
所以基本上在使用被互相连接3台世界上一个非常简化的视图。现在,为了得到我所需要的所有信息,我可以简单地执行查询像
select
w.name,
c.name,
ci.name
from
world w
left outer join country c on (w.id = c.world_id)
left outer join city ci on (c.id = ci.country_id)
这给我回了必要的数据:
name | name | name
-------+---------+----------
World | Austria | Vienna
World | Austria | Salzburg
World | Poland | Warsaw
World | Poland | Stetin
对于这样一个小例子,数据复制在结果集中可能没关系。尽管如此,在我的真实世界中,结果集要大得多(200k行),并且需要查询更多的列。由于整个结构的树形结构的提醒,我想知道,如果有可能建立一个结果集看起来更像这个有点:
id | parent | name | population
-------+----------+----------+------------
1 | null | World | 7125000000
2 | 1 | Austria | 8000000
3 | 1 | Poland | 38530000
4 | 2 | Vienna | 1741000
5 | 2 | Salzburg | 145000
6 | 3 | Warsaw | 1710000
7 | 3 | Stetin | 409000
所有ID都是不同的表中是唯一的。只有一个表来表示和构建树结果集,我读过,可以使用with-queries,但到目前为止,我没有找到任何有关如何实现这个涉及多个表的任何表。
这些表仍然通过外键“连接”,并且它可以仅查询子树(让我们说一切在波兰之下)? – u6f6o
我也想过有一个带有鉴别器的表格,但遗憾的是我无法轻松地在我们的产品系统上做出这样的模式变更。 – u6f6o
@ u6f6o:只需查询子树,就可以更改第二个CTE的非递归部分的条件,例如, 'name ='Poland''而不是'parent_id为null' –