别名

时间:2017-09-06 15:00:23

标签: sql postgresql sequelize.js

我对下一个SQL查询有问题(由sequelize ORM生成):

SELECT 
    "Publication".*, 
    "user"."id" AS "user.id"
FROM 
    (SELECT "Publication"."club_id" AS "clubId", "Publication"."user_id" AS "userId" FROM "publication" AS "Publication" WHERE ("Publication"."club_id" = '1')) AS "Publication" 
    LEFT OUTER JOIN "user_account" AS "user" ON "Publication"."user_id" = "user"."id";

产生的错误是:

  

错误:列Publication.user_id不存在

     

第6行:LEFT OUTER JOIN“user_account”AS“user”ON“Publication”。“...                                                            ^

我对正在发生的事情的直觉是"Publication"."user_id"被别名为userId。然后,查询被别名为"Publication"(覆盖之前引用该表的标签)。左外连接尝试将新Publication user_id属性与user.id匹配,但它无法使用,因为现在它被称为userId

我是否正确理解了这个问题?

1 个答案:

答案 0 :(得分:3)

  

错误:列 Publication.user_id 不存在

答案很简单user_id<> userId

SELECT "Publication".*, 
       "user"."id" AS "user.id"
FROM 
    (SELECT "Publication"."club_id"  AS "clubId"
            ,"Publication"."user_id" AS "userId" 
      FROM "publication" AS "Publication" 
     WHERE "Publication"."club_id" = '1') AS "Publication" 
LEFT JOIN "user_account" AS "user" 
  ON "Publication"."userId" = "user"."id";
                    -- here use column's alias