Using an Alias column in the where clause in Postgresql

后端 未结 6 1105
轮回少年
轮回少年 2020-11-22 11:04

I have a query like this:

SELECT
    jobs.*, 
    (
        CASE
            WHEN lead_informations.state IS NOT NULL THEN lead_informations.state
                   


        
6条回答
  •  不思量自难忘°
    2020-11-22 11:24

    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
    

提交回复
热议问题