多个内部加入别名不起作用

时间:2017-03-14 00:58:32

标签: sql postgresql

我无法理解SQL连接的语法。这是我的查询。

SELECT users.family_name, users.given_name, users.bio, votes_stories.*, stories.* \
    FROM stories \
    INNER JOIN users \
      ON users.id=stories.author_id \
    INNER JOIN votes_stories.type AS vote_type \ // <--- error occurs here. 
      ON votes_stories.story_id=stories.id \
      AND votes_stories.user_id=$1

错误:错误:关系“votes_stories.type”不存在

我想将vote_stories.type重命名为vote_type

2 个答案:

答案 0 :(得分:1)

我想你想要这样的事情:

SELECT u.family_name, u.given_name, u.bio, vs.type AS vote_type
FROM stories s INNER JOIN
     users u
     ON u.id = s.author_id INNER JOIN
     votes_stories vs
     ON vs.story_id = s.id AND
        vs.user_id = $1;

您要选择的列位于SELECT

答案 1 :(得分:0)

你走了:

SELECT users.family_name, users.given_name, users.bio, vote_type.*, stories.*
FROM stories
JOIN users ON users.id=stories.author_id
JOIN vote_type.type AS vote_type ON vote_type.story_id=stories.id AND vote_type.user_id=$1