mysql case when語句的使用

2021-10-02 13:03:45 字數 924 閱讀 5480

case具有兩種格式。簡單case函式和case搜尋函式。

簡單函式

case [col_name] when [value1] then [result1]…else [default] end

搜尋函式

case when [expr] then [result1]…else [default] end

簡單函式的運用

case [col_name] when [value1] then [result1]…else [default] end

列舉這個字段所有可能的值*

我們先看錶中原始資料

執行它select

case username

when 'admin' then '超級管理員'

when 'admin2' then '2管理'

when 'admin3' then '3管理'

else '其他普通使用者' end

2. 搜尋函式

case when [expr] then [result1]…else [default] end:搜尋函式可以寫判斷,並且搜尋函式只會返回第乙個符合條件的值,其他case被忽略

可以參考這個例子  

MySQL CASE WHEN的高階用法

case when condition1 then result1 when condition2 then result2 when conditionn then resultn else result end mysql select from person id country name a...

Mysql casewhen的三種用法

case when 的三種用法 1.case 字段 when 欄位的具體值。select a.case name when 流浪 then 法師 else 戰士 end as 型別 from c 20170920 a 2.case when 字段,這個可以對欄位進行取範圍。select a.case...

語句 switch語句

switch語句的特點如下 1 switch x 被選擇的內容 即x 只能是byte,short,int,char這四種型別 2 備選答案並沒有指定的順序,但是執行肯定是從第乙個case開始的,如果其中有匹配的case,執行完,通過該case的break就結束了switch。如果沒有匹配的case,...