【问题标题】:ILIKE and NOT ILIKE in aws redshift different from totalaws红移中的ILIKE和NOT ILIKE与总数不同
【发布时间】:2013-06-28 19:07:53
【问题描述】:

我在 amazon redshift 中运行了以下三个查询:

select count(*)  
from t1  

计数是 1554。

select count(*)  
from t1  
where  
    item_name ilike "blue"  

计数是 62。

select count(*)  
from t1  
where  
    item_name not ilike "blue"  

计数是 85。

最后两个 (62 + 85) 应该等于 1554。我错过了什么?

【问题讨论】:

    标签: sql count null amazon-redshift


    【解决方案1】:

    双引号用于标识符:"myColumn"
    单引号用于值:'value'

    您的示例与basic syntax rules 的示例相矛盾。

    另外,您没有考虑NULL 值,它们都不符合:

    item_name ilike 'blue'
    

    也不是:

    item_name not ilike 'blue'
    

    你得到了什么:

    SELECT count(*)                             AS all_rows
         , count(item_name  ~~* 'blue' OR NULL) AS item_name_blue
         , count(item_name !~~* 'blue' OR NULL) AS item_name_not_blue
         , count(item_name)                     AS item_name_not_null
         , count(item_name IS NULL OR NULL)     AS item_name_null
    FROM   t1;
    

    ~~* .. ILIKE 的内部 Postgres 运算符
    !~~* .. NOT ILIKE 的内部 Postgres 运算符
    (小心:运算符优先级略有不同。)

    【讨论】:

    • 谢谢。双引号是问题中的错字。一旦我添加了 null 来弥补差异。
    • 对于使用此作为参考点的任何人,~~* 运算符对应于ILIKE,而不是根据 PostgreSQL 文档 9.7.1 的LIKE
    • @John:谢谢,我修正了错字。 (顺便说一句:没有 Postgres 9.7 版。)
    • @ErwinBrandstetter 好收获!我显然指的是 Postgres 12.4 文档中的节号 9.7.1
    猜你喜欢
    • 1970-01-01
    • 2022-01-04
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 2018-05-13
    • 2022-01-27
    • 2019-07-23
    相关资源
    最近更新 更多