使用Oracle的Decode函式進行多值判斷

2021-08-31 05:11:43 字數 933 閱讀 6239

decode函式比較表示式和搜尋字,如果匹配,返回結果;如果不匹配,返回default值;如果未定義default值,則返回空值。以下是乙個簡單測試,用於說明decode函式的用法:

select pv.name, pv.cost_type, decode(pv.cost_type, 2, '包時段', '按次扣費') decode from product_version pv order by pv.product_id

[img]

select pv.name, pv.cost_type, decode(pv.cost_type, 2, '包時段') as decode from product_version pv order by pv.product_id

[img]

[b]使用decode函式來減少處理時間[/b]

使用decode函式可以避免重複掃瞄相同記錄或重複連線相同的表. 例如:

select count(pv.product_id) from product_version pv where pv.cost_type=2

[img]

select count(pv.product_id) from product_version pv where pv.cost_type=1

[img]

你可以用decode函式高效地得到相同結果:

select count(decode(pv.cost_type, 2, '包時段', null)),count(decode(pv.cost_type, 1, '按次扣費', null)) from product_version pv

[img]

oracle中的decode的使用

oracle 中 decode 函式用法 含 釋 decode 條件,值1,返回值1,值2,返回值2,值n,返回值n,預設值 該函式的含義如下 if 條件 值1 then return 翻譯值1 elsif 條件 值2 then return 翻譯值2 elsif 條件 值n then return...

oracle中的decode的使用

oracle 中 decode 函式用法 含 釋 decode 條件,值1,返回值1,值2,返回值2,值n,返回值n,預設值 該函式的含義如下 if 條件 值1 then return 翻譯值1 elsif 條件 值2 then return 翻譯值2 elsif 條件 值n then return...

oracle中的decode的使用

oracle 中 decode 函式用法 含 釋 decode 條件,值1,返回值1,值2,返回值2,值n,返回值n,預設值 該函式的含義如下 if 條件 值1 then return 翻譯值1 elsif 條件 值2 then return 翻譯值2 elsif 條件 值n then return...