且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-04 23:14:18

如您所见,MySQL 的支持是非标准的.正确的方法是重新打印SELECT子句中使用的相同表达式:

MySQL's support is, as you experienced, non-standard. The correct way is to reprint the same expression used in the SELECT clause:

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