1. 程式人生 > >sql case when then的使用

sql case when then的使用

sql case when then的使用

base_dict 表中有如下的資料

如果檢索的是dict_type_code, 那麼可以使用case when then else end 對其再次賦值
使用case when then 檢索

SELECT (case dict_type_code 
				when 001 then '客戶行業' 
				WHEn 003 then '公司性質'
				when 004 then '年營業額'
        else '其他'
    end 
) type from base_dict;

檢索出來的結果如下

可以看到,結果值不為code值, 而是對應的then後面的值.
對於其他沒有寫when的code值, 顯示為其他.
注意要寫end ,否則會報錯.