WHERE clause great than zero still showing zero when using CASE functions

前端 未结 1 1085

I am trying the WHERE clause to filter out other data I dont want.

SELECT `post_id`,
       MAX(CASE WHEN `meta_key` = \'vlaue_1\' THEN `meta_value` ELSE NULL END         


        
1条回答
  •  攒了一身酷
    2021-01-27 11:37

    Your WHERE logic belongs in a HAVING clause:

    SELECT
        post_id,
        MAX(CASE WHEN meta_key = 'vlaue_1' THEN meta_value END) AS Customer,
        MAX(CASE WHEN meta_key = 'value_2' THEN meta_value END) AS DeliveryDate,
        MAX(CASE WHEN meta_key = 'value_3' THEN meta_value END) AS DeliveryTime,
        MAX(CASE WHEN meta_key = 'value_4' THEN meta_value END) AS DeliveryType
    FROM wp_postmeta 
    GROUP BY post_id
    HAVING Customer > 0
    ORDER BY post_id;
    

    Note that your current WHERE clause might technically be valid MySQL:

    WHERE 'Customer' > 0
    

    However, this checks whether the string literal 'Customer' is greater than the value 0. It does not actually check the value of the customer case expression, which is not even available until after group by happens.

    0 讨论(0)
提交回复
热议问题