ILIRE和NOT ILIKE在aws redshift中与总数不同

时间:2013-06-28 19:07:53

标签: sql postgresql count null amazon-redshift

我在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.我错过了什么?

1 个答案:

答案 0 :(得分:2)

双引号用于标识符:"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_nameIS NULL OR NULL)      AS item_name_null
FROM   t1;

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