decode,case when 有什么區別?各自的用法?
uj5u.com熱心網友回復:
case when比decode功能要強大,等到你遇到decode實作不了的功能,你就想到了case when了。uj5u.com熱心網友回復:
decode 是等值比較,使用語法簡單,不用寫太長。case when 有兩比較方式,但是語法上要復雜一些。
-- 條件非常靈活
case
when 條件1 then 值1
when 條件2 then 值2
else 值3
end
-- 等同于 decode
case 值
when 值1 then 值11
when 值2 then 值22
else 值33
end
uj5u.com熱心網友回復:
1.對null的處理不同2.decode要敲的字少
uj5u.com熱心網友回復:
case when比decode更強大,適合多條件,且條件比較復雜的情況。如果條件比較簡單decode,會使代碼更簡潔。
uj5u.com熱心網友回復:
對一個特定值比較用decode,范圍比較用case whenuj5u.com熱心網友回復:
補充:case值when方式時,這里的decode(null,null,1)可以回傳1,但是case null when null then 1 end 不能回傳1uj5u.com熱心網友回復:

uj5u.com熱心網友回復:
我覺得decode 就是縮減版本的case when,判斷邏輯簡單用decode ,判斷邏輯復雜用case when,case when 3個以上判斷代碼可讀性更高。uj5u.com熱心網友回復:
這個沒啥好糾結的,decode是ORACLE特有的寫法方便,而case when是基于sql語法標準的寫法,基本所有資料庫都支持uj5u.com熱心網友回復:
case when 很大優點是 when 后面可以是運算式,而decode判斷只能用具體的值。uj5u.com熱心網友回復:
case when 功能比decode 強大。decode可以實作的功能,case when 都可以。但是,反之不一定。
uj5u.com熱心網友回復:
Case when 理解性,可讀性比較好,一看就知道邏輯,且Oracle 和MS SQL 都可用decode 則要想一下。
同事用 decode 寫了一堆 decode 組句,過了一星期要debug,已無法判別了,費了好多時間才改好
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/41664.html
標籤:開發
下一篇:關于ORACLE資料庫視圖的問題
