sql - aws redshift 中的 ILIKE 和 NOT ILIKE 与 total 不同

标签 sql 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。我错过了什么?

最佳答案

双引号用于标识符:"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 运算符 (注意:运算符优先级略有不同。)

关于sql - aws redshift 中的 ILIKE 和 NOT ILIKE 与 total 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17371926/

相关文章:

php mysql连接使用

mysql - 仅当值不存在时返回行

python - 计算文件中的大写字母、小写字母、数字和空格

php - 复杂(ish)SQL 连接和计数查询

sql - 标准 SQL bool 运算符 IS 与等于 (=) 运算符

sql - 连接和计算来自不同表的行

使用连接的 SQL 更新查询

mysql - SQL 查询为每个用户提供唯一字段和计数

javascript - 另一个 Javascript 未定义空问题

macos - 为什么我的 FinderSync 扩展在启动之前就崩溃了?