2016-11-07 21 views
1

我不明白爲什麼有時LIKE需要ANY和其他時候它需要ALL,這讓我發瘋。我覺得我應該能夠在任何條件下使用ANY(我試圖在括號中的任何正則表達式之後選擇記錄)。sql:像任何vs像所有

出於某種原因,第一個LIKE與ANY一起工作得很好 - 它會返回所有帶有狗食,血統或善良的記錄。

然而,第二個LIKE需要ALL。否則,它不會留下治療,用品或潮溼的記錄。但爲什麼?我覺得在這裏任何一個都是合適的形式。

where dsc_item like any ('%DOG CHOW%','%PEDIGREE%','%BENEFUL%') 
and dsc_comm not like all ('%TREATS%','%SUPPLIES%', '%WET%') 

回答

3

LIKE ANY轉化爲OR ED條件,但LIKE ALLAND

where 
( dsc_item like '%DOG CHOW%' 
    OR dsc_item like '%PEDIGREE%','%BENEFUL%' 
) 
and 
( dsc_comm not like '%TREATS%' 
    AND dsc_comm not like '%SUPPLIES%' 
    AND dsc_comm not like '%WET%' 
) 

如果更換ANDOR它就像col <> 1 OR col <> 2這是每一個非空行真。

0
like any  similar to  = any 
like all  similar to  = all 
not like any similar to  <> any 
not like all similar to  <> all 

select  'My name is Inigo Montoya, you killed mhy father, prepare to die!' as str 

      ,case when str like any ('%Inigo%','%Donald%' ,'%Hillary%')  then 1 else 0 end -- 1 
      ,case when str like any ('%Adam%' ,'%Donald%' ,'%Hillary%')  then 1 else 0 end -- 0 
      ,case when str like all ('%Inigo%','%Montoya%','%father%')  then 1 else 0 end -- 1 
      ,case when str like all ('%Inigo%','%Montoya%','%mother%')  then 1 else 0 end -- 0 

      ,case when str not like any ('%Inigo%','%Montoya%','%mother%') then 1 else 0 end -- 1 
      ,case when str not like any ('%Inigo%','%Montoya%','%father%') then 1 else 0 end -- 0 
      ,case when str not like all ('%Adam%' ,'%Donald%' ,'%Hillary%') then 1 else 0 end -- 1 
      ,case when str not like all ('%Inigo%','%Donald%' ,'%Hillary%') then 1 else 0 end -- 0 
;