微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Postgresql中的where子句中使用别名列

我有一个这样的查询
SELECT jobs.*,(CASE WHEN lead_informations.state IS NOT NULL THEN lead_informations.state ELSE 'NEW' END) as lead_state
FROM "jobs"
LEFT JOIN lead_informations
  ON lead_informations.job_id = jobs.id
  AND lead_informations.mechanic_id = 3
WHERE (lead_state = 'NEW')

其中出现以下错误

PGError: ERROR:  column "lead_state" does not exist
LINE 1: ...s.id AND lead_informations.mechanic_id = 3 WHERE (lead_state...

MysqL这是有效的,但显然不是在Postgresql。从我可以收集,原因是查询的SELECT部分​​的评估晚于WHERE部分。有这个问题的常见解决方法吗?

MysqL支持是,因为你体验到,非标准。正确的方法是重新打印在SELECT子句中使用的同一个表达式:
SELECT jobs.*,CASE 
         WHEN lead_informations.state IS NOT NULL THEN lead_informations.state 
         ELSE 'NEW' 
       END as lead_state
  FROM "jobs"
LEFT JOIN lead_informations ON lead_informations.job_id = jobs.id
                           AND lead_informations.mechanic_id = 3
    WHERE lead_informations.state IS NULL

原文地址:https://www.jb51.cc/postgresql/193509.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐