2017-08-31 76 views
0

所以我有一個查詢,我試圖獲取列或generic列中包含單詞citalopram或單詞celexa的行。where子句沒有在Postgres查詢中選擇正確的行

首先我想:

where  
    ( medication  ilike '%citalopram%' or 
     generic  ilike '%citalopram%' or 
     medication  ilike '%celexa%'  or 
     generic  ilike '%celexa%') 

,一切都找到和花花公子,除了包含在藥物escitalopram,它也選擇了行。所以我改變了查詢,以便它看起來像下面這樣:

where  
    ( medication  ilike '%citalopram%' or 
     generic  ilike '%citalopram%' or 
     medication  ilike '%celexa%'  or 
     generic  ilike '%celexa%')   and 
     medication not ilike '%escitalopram%' and 
     generic not ilike '%escitalopram%' 

現在,果然它消除含escitalopram行,但現在有時會忽略有行:

  • 服藥= CeleXa 40mg oral tablet,通用= null
  • 藥物= citalopram 40mg oral tablet,通用= null

不知道爲什麼這可能是問題。任何建議將不勝感激!

+2

這是因爲通用有一個空值 - 值得一讀此HTTPS: //stackoverflow.com/questions/22818070/behaviour-of-not-like-with-null-values –

+0

你可能會更好使用正則表達式進行「整個單詞」搜索,例如'藥物〜*'\ ycelexa \ y'' –

回答

0

爲什麼不:

where 
concat(' ',medication,' ',generic) ilike '% citalopram%' 
    or 
concat(' ',medication,' ',generic) ilike '% celexa%' 

把空間柱之前,因此,如果列值字啓動時,它仍然有空間