DECODE在WHERE語句中的妙用

2022-08-30 11:27:13 字數 3337 閱讀 7393

假設我們有兩個表,需要用row_id連線,找出值相同或者不相同的資料

with temp1 as(

select 1 as row_id,1 as cola from dual

union all select 2 ,1 from dual

union all select 3 ,null from dual

union all select 4 ,null from dual

)select * from temp1;

with temp2 as(

select 1 as row_id,1 as colb from dual

union all select 2,null from dual

union all select 3,1 from dual

union all select 4,null from dual

)select * from temp2;

兩個表的結果集如圖:

我們對比較結果的期望如列5,如果:

null值的存在有點繞人,

null=null 結果為null

1=null結果為null

所以必須對當欄位可為null的時候,比較需要對null進行單獨的處理

找出cola和colb相等的記錄

不使用decode,必須對null進行單獨的處理

with temp1 as(

select 1 as row_id,1 as cola from dual

union all select 2 ,1 from dual

union all select 3 ,null from dual

union all select 4 ,null from dual

),temp2 as(

select 1 as row_id,1 as colb from dual

union all select 2,null from dual

union all select 3,1 from dual

union all select 4,null from dual

)select * from temp1 a

inner join temp2 b

on a.row_id=b.row_id

where a.cola=colb

or (a.cola is null and b.colb is null) /*必須對null進行單獨處理*/

使用decode替換:

with temp1 as(

select 1 as row_id,1 as cola from dual

union all select 2 ,1 from dual

union all select 3 ,null from dual

union all select 4 ,null from dual

),temp2 as(

select 1 as row_id,1 as colb from dual

union all select 2,null from dual

union all select 3,1 from dual

union all select 4,null from dual

)select * from temp1 a

inner join temp2 b

on a.row_id=b.row_id

where decode(a.cola,b.colb,1,0)=1

結果集如圖:

找出cola和colb不相等的記錄

不使用decode,需要對null進行一大段處理

with temp1 as(

select 1 as row_id,1 as cola from dual

union all select 2 ,1 from dual

union all select 3 ,null from dual

union all select 4 ,null from dual

),temp2 as(

select 1 as row_id,1 as colb from dual

union all select 2,null from dual

union all select 3,1 from dual

union all select 4,null from dual

)select * from temp1 a

inner join temp2 b

on a.row_id=b.row_id

where a.cola!=colb

or (a.cola is null and b.colb is not null)

or (a.cola is not null and b.colb is null) /*必須對null進行單獨處理*/

使用decode

with temp1 as(

select 1 as row_id,1 as cola from dual

union all select 2 ,1 from dual

union all select 3 ,null from dual

union all select 4 ,null from dual

),temp2 as(

select 1 as row_id,1 as colb from dual

union all select 2,null from dual

union all select 3,1 from dual

union all select 4,null from dual

)select * from temp1 a

inner join temp2 b

on a.row_id=b.row_id

where decode(a.cola,b.colb,1,0)=0

結果集如圖:

盡量避免在SQL語句的WHERE子句中使用函式

start 在sql語句的where子句中應該盡量避免在字段上使用函式,因為這樣做會使該字段上的索引失效,影響sql語句的效能。即使該字段上沒有索引,也應該避免在字段上使用函式。考慮下面的情況 現在要求你把2009.9.24註冊的使用者都查出來,怎麼辦?可能有人會這麼寫 不過很遺憾,這個語句是錯誤的...

sql語句中where的引號用法

因為 ip 沒有加單引號 請注意,我們在例子中的條件值周圍使用的是單引號。sql 使用單引號來環繞文字值 大部分資料庫系統也接受雙引號 如果是數值,請不要使用引號。這是正確的 select from persons wherefirstname bush 這是錯誤的 select from pers...

SQL語句中where和 on的區別

join過程可以這樣理解 首先兩個表做乙個笛卡爾積,on後面的條件是對這個笛卡爾積做乙個過濾形成一張臨時表,如果沒有where就直接返回結果,如果有where就對上一步的臨時表再進行過濾。下面看實驗 先準備兩張表 先執行inner join 結果沒有區別,前者是先求笛卡爾積然後按照on後面的條件進行...