SQL中的if else的使用

2021-10-09 17:26:09 字數 1154 閱讀 1395

1.oracle中if…else的方法:

第一種:寫表示式

select

case

when 2>1 then '真'

else '假'

end as we

from dual

第二種:固定

select

case 1

when 1 then '欄位的值是1'

when 2 then '欄位的值是2'

else '欄位的值3'

end as we

from

dual

第三種:decode(需要判斷的值,判斷1的結果條件(if),符合判斷1的結果,判斷2的結果條件(else if),符合判斷2的結果,(else))

select

decode(2, 1, '男', 2, '女', '未知')

from

dual

注意:nvl()是oracle資料庫中對字段的非空校驗,如果欄位名為空,則賦值為逗號後面的值。

2.mysql中if…else的方法

第一種:

select

if(1>0, '真', '假')

from

dual

也可以多條件巢狀用:

select

if(1>0, if(2>1, '真', '假'), '假')

from

dual

第二種:

select

case 1

when 1 then '欄位的值是1'

when 2 then '欄位的值是2'

else '欄位的值3'

end as we

from

dual

也可以這樣寫:

select

case 1

when 1 then '欄位的值是1'

when 2 then '欄位的值是2'

else '欄位的值3'

end as we

from

dual

注意:ifnull("欄位名", 值) -- 非空驗證 如果欄位為空就輸出後面的值

優雅的使用if else

簡單的if else if 還好,就怕if else if 的裡面給你整個俄羅斯套娃。畫面太美 判斷書本狀態 private void testif string status else if done equals status else if read equals status 等到上面的場景...

SAS巨集中 if else和if else的區別

sas巨集中 if else和if else的區別 data a set x do m 1 to 3 do q 1 to 5 if x q nan and m id and x q id m q then do id m q 1 put 有值 且id相等 id x q id m q 上面 是正常的i...

批處理IF ELSE的使用

if performs conditional processing in batch programs.在批程式中執行條件處理。if not errorlevel number command if not string1 string2 command if not exist filename...