資料庫 Oracle條件判斷語句

2022-06-11 09:21:09 字數 2467 閱讀 4427

經常有這種需求,如當資料庫裡的狀態為20100時,中文希望顯示為辦理成功,這個時候就需要條件判斷了

oracle中條件判斷有三種,如下將分別介紹

select case

when t.a = 1 then

'成功'

when t.a = 2 then

'失敗'

else

'其他'

end test_clo

from table t

-- 注:

a為表t中的列名,後面的意思是當a的值為1時翻譯為'成功',當2時,翻譯為'失敗',不是這兩種就翻譯為'其他'

test_clo 為上面a的值翻譯的值的列名

# 需求:按對應狀態分組獲取對應的總數,且狀態顯示對應的中文

select t.order_status 訂單狀態編碼,

count(1) 分類總數,

case

when t.order_status = '20100' then

'辦理中'

when t.order_status = '20101' then

'辦理成功'

when t.order_status = '20102' then

'辦理失敗'

else

'其他'

end 訂單狀態

from base_order t

where t.order_createtime >=

to_timestamp('2020-02-14 14:30:00', 'yyyy-mm-dd hh24:mi:ss')

group by order_status

select decode(columnname,值1,翻譯值1,值2,翻譯值2,...值n,翻譯值n,預設值)

from talbename

where …

-- 注:

其中:columnname為要選擇的table中所定義的column;

預設值可以是你要選擇的column name本身,也可以是你想定義的其他值,比如other等;

主要作用:相當於if語句, 將查詢結果翻譯成其他值。(即以其他形式表現出來)。

現定義一table名為output,其中定義兩個column分別為monthid(var型)和sale(number型),若sale值=1000時翻譯為d,=2000時翻譯為c,=3000時翻譯為b,=4000時翻譯為a,如是其他值則翻譯為other

select monthid,

decode(sale, 1000, 'd', 2000, 'c', 3000, 'b', 4000, 'a', 』other』) test_sale

from output

若只與乙個值進行比較:

select monthid, decode(sale, null, '---' ,sale) test_sale from output
decode中可使用其他函式,如nvl()函式或sign()函式等:

nvl(expr1,expr2)

若expr1是null,則返回expr2,否則返回expr1。

sign()函式根據某個值是0、正數還是負數,分別返回0、1、-1,

用如下的sql語句取較小值:

select monthid, decode(sign(sale - 6000), -1, sale, 6000) from output;
select decode(t.order_status,

'20100',

'辦理中',

'20101',

'辦理成功',

'20102',

'辦理失敗',

order_status) 訂單狀態,

count(t.order_status)

from base_order t

group by order_status

if con = '1' then

'滿意'

end if;

if con = '1' then

'滿意'

else

'其他'

end if;

if con = '1' then

'滿意'

elsif con = '2' then

'一般'

else

'不滿意'

end if;

if條件判斷語句

如果表示式的值是true,則執行語句塊 否則跳過語句塊。equals 下面的例子使用到的,可以看看。字串判斷不能使用 要使用方法。用來判斷記憶體位址是否相等。輸入男女,輸出boy,girl system.out.println 請輸入男or女 scanner sc new scanner syste...

條件判斷語句

語法 if condition else if condition else示例 var age 20 if age 18 else語法 表示式1 表示式2 表示式3 如果表示式1的布林值為true,則執行表示式2,否則執行表示式3。是js語言中唯一乙個需要三個運算元的運算子。示例 var age ...

條件判斷語句

條件判斷語句 使用條件判斷語句可以在執行某個語句之前進行判斷,如果條件成立才會執行語句,條件不成立則語句不執行。if語句 語法一 if 條件表示式 if語句在執行時,會先對條件表示式進行求值判斷,如果條件表示式的值為true,則執行if後的語句,如果條件表示式的值為false,則不會執行if後的語句...