【问题标题】:where clause not selecting proper rows in a Postgres querywhere 子句没有在 Postgres 查询中选择正确的行
【发布时间】:2017-08-31 03:41:09
【问题描述】:

所以我有一个查询,我试图在medication 列或generic 列中获取包含单词citalopram 或单词celexa 的行。

首先我尝试过:

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

等等

不知道为什么这可能是问题所在。任何建议将不胜感激!

【问题讨论】:

标签: postgresql


【解决方案1】:

为什么不:

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

在列之前放置空格,因此如果列值以单词开头,它仍然会有空格

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-04
    • 2011-07-05
    • 1970-01-01
    • 2012-02-17
    • 2019-08-27
    • 2018-03-06
    • 1970-01-01
    • 2012-10-09
    相关资源
    最近更新 更多