我正在编写一个用作数据库视图的查询,现在看起来像这样
SELECT
contact.*,
contact_users.names AS user_names,
contact_status.status_id AS status_id,
status_translation.name AS status_name,
status_translation.lang_id AS lang_id
FROM contacts as contact
LEFT JOIN contact_status AS contact_status ON contact_status.status_id = contact.status
LEFT JOIN contact_status_translation AS status_translation ON status_translation.id = contact.status
LEFT JOIN (
SELECT
contacts_users.contact_id,
string_agg(users.fullname || ', ' || users.id, ' | ') as names
FROM v_contacts_users as contacts_users
LEFT JOIN v_users as users on users.id = contacts_users.user_id
WHERE users.lang_id = status_translation.lang_id
GROUP BY contacts_users.contact_id
) AS contact_users ON contact_users.contact_id = contact.id
WHERE contact.deleted = FALSE
除了上一个WHERE
中的LEFT JOIN
条件之外,所有内容都按预期工作 - WHERE users.lang_id = status_translation.lang_id
部分表示在此部分查询中无法引用status_translation
?这是为什么?我尝试用各种各样的方式引用这个表,但结果仍然是相同的.Thing是v_users
被翻译,所以我只需要从这个表中得到一个结果。感谢任何提示
答案 0 :(得分:1)
如果您想引用之前的LATERAL
列表条目,请在LEFT JOIN
和左括号之间插入FROM
。