mysql - SQL 多个 NOT LIKE 可能包含 NULL 值

标签 mysql null sql-like

我正在尝试提取多个不同于“%1%”、“%2%”和“%3%”字段的数据

但是,这些字段中的一个或多个很可能为 NULL,这导致它们在结果中被忽略。我也想要带有 NULL 值的结果。我看到 data1 = 5、data2 = 6 和 data3 为 NULL 的实例,并且这些记录未返回。

这是我的查询:

WHERE
region = 'AZ' and
year(date) = '2014' and

data1 not like '%1%' and
data2 not like '%1%' and
data3 not like '%1%' and

data1 not like '%2%' and
data2 not like '%2%' and
data3 not like '%2%' and

data1 not like '%3%' and
data2 not like '%3%' and
data3 not like '%3%'

最佳答案

在末尾添加 OR 子句

WHERE
region = 'AZ' and
year(date) = '2014' and
(
data1 not like '%1%' and
data2 not like '%1%' and
data3 not like '%1%' and

data1 not like '%2%' and
data2 not like '%2%' and
data3 not like '%2%' and

data1 not like '%3%' and
data2 not like '%3%' and
data3 not like '%3%'
OR data1 IS NULL OR data2 IS NULL OR data3 IS NULL
)

关于mysql - SQL 多个 NOT LIKE 可能包含 NULL 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29542323/

相关文章:

php - 从多表中选择语句

MySQL 在 .SQL 转储中的使用

java - Eclipse null 分析对类文字发出 null 类型安全警告

mysql - 为什么在 Doctrine 1.1.2 中使用 DQL 将 "s.d IS NULL"添加到我的 where 子句中(使用 leftJoin)?

sql-server - SQL NOT LIKE 和 LIKE

php - 查询中的问题(PHP MYSQL UTF-8)

php - mySql为购物车选择多个可能重复ID的ID

ruby-on-rails - Rails 选择具有 NULL 值的 bool 值

带有多个 LIKE 运算符的 Hive 查询

php - MySQL选择流派问题(php)