我正在尝试创建可以从连接源中累积所有所需数据的视图:
CREATE OR REPLACE VIEW dir AS
SELECT
dir_data.id,
dir_data.parent_id,
dir_data.name,
(owner.*)::owner, -- owner_id
FROM
dir_data
LEFT JOIN owner ON owner.id = dir_data.owner_id
例如,这允许以简单的方式选择所有者的数据:
SELECT
id,
name,
(owner).id AS owner_id,
(owner).name AS owner_name,
((owner).company).name AS owner_company
FROM
dir
WHERE
id = 7
问题是我需要使用视图进行自连接dir
(这是正在创建的视图)以parent_id
类似的方式转换字段。PostgreSQL 似乎不喜欢它,它说relation "dir" does not exist
.
有什么提示吗?
对 Marcelo Cantos 评论的回答:
CREATE OR REPLACE VIEW dir AS
SELECT
...
FROM
dir_data
LEFT JOIN owner ON owner.id = dir_data.owner_id -- "standard" join
LEFT JOIN dir AS parent_dir ON parent_dir.id = dir_data.parent_id -- self-join, does not work