When using SELECT can you modify the value of a returned field based on other fields?

后端 未结 4 1819
梦毁少年i
梦毁少年i 2021-01-18 02:44

When using MySQL SELECT can you change the value of a returned field based on other fields?

For example, if I have this select:

SELECT city,state,cou         


        
4条回答
  •  天命终不由人
    2021-01-18 03:14

    Update(misprints corrected):

    SELECT city,state,
    CASE 
     WHEN (city IS NULL OR city='') AND (state IS NULL or state='') THEN ''
     ELSE country
    END as country_1
     FROM `table`
    

    You can also use IF instead of CASE:
    IF ((city IS NULL OR city='') AND (state IS NULL or state=''),'',country) as country_1

提交回复
热议问题