创建Postgres View时遇到错误:列“ id”多次指定

时间:2019-12-10 20:04:46

标签: sql postgresql sql-view

场景:

我有一条select语句,JOIN将一堆表放在一起:

SELECT 
    e0.id, e0.name, e0.slug,
    e1.id, e1.edition, e1.url, e1.date, e1.event_id,
    v2.id, v2.title, v2.language, v2.description, v2.provider, v2.videoid, v2.image_url, v2.event_id, v2.edition_id,
    s3.id, s3.name, s3.twitter, s3.website
FROM 
    events AS e0 
LEFT OUTER JOIN 
    editions AS e1 ON e1.event_id = e0.id
LEFT OUTER JOIN 
    videos AS v2 ON v2.edition_id = e1.id
LEFT OUTER JOIN 
    videos_speakers AS v4 ON v4.video_id = v2.id
LEFT OUTER JOIN 
    speakers AS s3 ON v4.speaker_id = s3.id
ORDER BY 
    e1.date DESC;

我想创建一个Postgres View。所以这样写出来:

CREATE VIEW all_events 
AS
    SELECT 
        e0.id, e0.name, e0.slug,
        e1.id, e1.edition, e1.url, e1.date, e1.event_id,
        v2.id, v2.title, v2.language, v2.description, v2.provider, v2.videoid, v2.image_url, v2.event_id, v2.edition_id,
        s3.id, s3.name, s3.twitter, s3.website
    FROM 
        events AS e0 
    LEFT OUTER JOIN 
        editions AS e1 ON e1.event_id = e0.id
    LEFT OUTER JOIN 
        videos AS v2 ON v2.edition_id = e1.id
    LEFT OUTER JOIN 
        videos_speakers AS v4 ON v4.video_id = v2.id
    LEFT OUTER JOIN 
        speakers AS s3 ON v4.speaker_id = s3.id
    ORDER BY 
        e1.date DESC;

我不断收到此错误:

  

错误:列“ id”指定了多次

问题:

  1. 如何解决此错误?我想创建一个名为“ all_events”的视图。
  2. 其他语言中的Postgres View是否类似于别名?

Postgres的新手,读过docs,但试图在此处理解心理模型。

2 个答案:

答案 0 :(得分:3)

您有几个相同的列名。即使您选择e0.id,该列仍然是名称(仅)id

但是在视图(或表)的范围内,每个列名必须唯一。

您需要为每个重复的列提供别名:

CREATE VIEW all_events AS
SELECT  e0.id as event_id, --<< here 
        e0.name as event_name, --<< here 
        e0.slug,
        e1.id as edition_id,  --<< here
        e1.edition, 
        e1.url, 
        e1.date, 
        e1.event_id as edition_event_id, --<< here
        v2.id as video_id,  --<< here
        v2.title, 
        v2.language, 
        v2.description, 
        v2.provider, 
        v2.videoid, 
        v2.image_url, 
        v2.event_id as video_event_id, --<< here 
        v2.edition_id as video_edition_id, --<< here
        s3.id as speaker_id,  --<< here
        s3.name as speaker_name, --<< here 
        s3.twitter, 
        s3.website
FROM events AS e0 
  LEFT OUTER JOIN editions AS e1 ON e1.event_id = e0.id
  LEFT OUTER JOIN videos AS v2 ON v2.edition_id = e1.id
  LEFT OUTER JOIN videos_speakers AS v4 ON v4.video_id = v2.id
  LEFT OUTER JOIN speakers AS s3 ON v4.speaker_id = s3.id;

尽管Postgres允许,但我强烈建议使用ORDER BY语句创建视图。如果您曾经按另一列对该视图的结果进行排序,则Postgres将对数据进行两次排序。

答案 1 :(得分:1)

在视图中,列的默认名称与查询的列相同。发生错误,因为在所有3个联接表中都定义了名为id的列。您需要提供别名以区分冲突列,例如:

CREATE VIEW all_events AS
SELECT e0.id as e0id, e0.name as e0name, e0.slug,
        e1.id as e1id, e1.edition, e1.url, e1.date, e1.event_id,
        v2.id as e2id, v2.title, v2.language, v2.description, v2.provider, v2.videoid, v2.image_url, v2.event_id, v2.edition_id,
        s3.id as s3id, s3.name as s3name, s3.twitter, s3.website
        FROM events AS e0 
        LEFT OUTER JOIN editions AS e1 ON e1.event_id = e0.id
        LEFT OUTER JOIN videos AS v2 ON v2.edition_id = e1.id
        LEFT OUTER JOIN videos_speakers AS v4 ON v4.video_id = v2.id
        LEFT OUTER JOIN speakers AS s3 ON v4.speaker_id = s3.id
        ORDER BY e1.date DESC;