Mysql case not working

后端 未结 1 1601
青春惊慌失措
青春惊慌失措 2021-01-29 06:34
SELECT 
     SQL_CALC_FOUND_ROWS a.* , 
     zn.`name` AS zone_name, 
     c.`name` AS carrier_name, 
     CASE type 
       WHEN type=1 THEN \'General day\' 
       ELS         


        
1条回答
  •  清酒与你
    2021-01-29 07:04

    When you put column name after CASE, you shouldn't use WHEN column = value, just use WHEN value, because it automatically compares the column to each value in the WHEN clauses.

    CASE type
        WHEN 1 THEN 'General day'
        ELSE 'Special date'
    END AS type_changed,
    CASE week_day 
       WHEN -1 THEN 'notset' 
       WHEN 1 THEN 'monday' 
       WHEN 2 THEN 'tuesday' 
       WHEN 3 THEN 'wednesday' 
       WHEN 4 THEN 'thursday' 
       WHEN 5 THEN 'friday' 
       WHEN 6 THEN 'saturday' 
       WHEN 7 THEN 'sunday' 
    END AS week_day_mod , 
    

    When you do both, you're testing week_day = (week_day = -1), week_day = (week_day = 1), etc. It works on Monday because 1 = (1 = 1) is equivalent to 1 = 1, which is true; but on Tuesday, it's 2 = (2 = 2), which is equivalent to 2 = 1, which is false.

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