case具有兩種格式笋颤。分別是case函數(shù)與case搜索函數(shù)。
--簡單case函數(shù)
case sex
when '1' then '男'
when '2' then '女’
else '其他' end
--case搜索函數(shù)
case when sex = '1' then '男'
when sex = '2' then '女'
else '其他' end
兩種方式可以實現(xiàn)相同的功能。但稍微有點區(qū)別。具體在工作中使用最方便快捷的方式解決問題,事后可以深入研究狞膘。
1.簡單case函數(shù)的寫法相對比較簡潔,但是和case搜索函數(shù)相比什乙,功能方面會有些限制挽封,比如寫判定式。
2.還有一個需要注重的問題臣镣,case函數(shù)只返回第一個符合條件的值辅愿,剩下的case部分將會被自動忽略智亮。
case when col_1 in ('a','b') then '第一類'
when col_1 in ('a') then '第二類'
else '其他' end
一般的case when語句的使用方法網(wǎng)上都有,但是相對于多條件的判斷就比較少点待,一般使用case 搜索函數(shù)
表
SXDIH{Q5TLIH~CQDI`4OJHC.png
SQL> select u.id u.nane namer= (
case when (u.name='張五' and u.sex='2') then '大哥')
when (u.name='張六' and u.sex='1') then '二哥')
when (u.name='張七' and u.sex='2') then '三哥')) from user u