sql 條件判斷

2021-08-30 14:25:09 字數 629 閱讀 4422

case具有兩種格式。分支結構case語句和條件判斷case語句。

分支結構case語句,類似於switch

select

(case user_name

when '張三' then 'zhangsan'

when '李四' then 'lisi'

when '王五1' then 'wangwu'

else '找不到對應' end

) as a

from ctl_user

條件判斷case,類似於if else

select

(case

when user_code > 0 and user_code < 100 then '一百以內'

when user_code > 100 and user_code < 1000 then '一千以內'

when user_code>1000 and user_code < 2000 then '一萬以內'

else '不在範圍' end

) as a

from ctl_user

注意兩者的寫法。

還有乙個需要注意的問題,分支結構case語句只返回第乙個符合條件的值,剩下的case部分將會被自動忽略。

SQL 判斷時間條件

1.判斷年月日是否相等 select from table where cast convert varchar 10 時間字段,120 as datetime 2010 10 01 判斷邏輯 通過convert函式現將時間欄位中年月日擷取出來,然後利用cast函式轉化為datetime。2.判讀度...

SQL條件判斷語句

select case when price is null then not yet priced when price 10 then very reasonable title when price 10 and price 20 then coffee table title else ex...

sql中使用if多條件判斷

1 以一表為例 bill 單據表 id billno status amount create date 1 gr0001 gr 155 2009 09 09 2 do0001 do 150 2009 09 09 2 so0001 so 153 2009 09 09 說明 status gr表示 進...