Mysql 如何选择此查询的逆查询?

Mysql 如何选择此查询的逆查询?,mysql,select,inverse,Mysql,Select,Inverse,如何选择此查询的反向,以及为什么尝试失败 要撤消的查询: SELECT * FROM table_name where (category_id is null and product_id is null) or request_path like '%Portal%'; 我的尝试: SELECT * FROM table_name where (category_id is not null and product_id is not null) and request_path n

如何选择此查询的反向,以及为什么尝试失败

要撤消的查询:

SELECT * FROM table_name 
where (category_id is null and product_id is null) or request_path like '%Portal%';
我的尝试:

SELECT * FROM table_name 
where 
(category_id is not null and product_id is not null)
and request_path not like '%Portal%';
当我尝试对每一行的结果进行count()并将它们相加时,我不会得到每一行的总计数()。它总是小于总数,所以我知道我没有选择相反的。我也不能证明我没有欺骗选择

假设category_id和product_id可以是整数,而request_path决不是string.empty或null

我是一名.net程序员。我可能可以很容易地在linq中实现这一点,但我正在修复一个mysql数据库。直接SQL一直是我的致命弱点

转换为SQL时,您将获得以下规则:

NOT (a AND b) = NOT a OR  NOT b
NOT (a OR  b) = NOT a AND NOT b
这就是为什么需要将
替换为

where (category_id is not null or product_id is not null)
  and request_path not like '%Portal%'
但我会这样写:

where coalesce(category_id, product_id) is not null
  and request_path not like '%Portal%'
更新

Trinimons注释是正确的:如果
请求_路径
可以包含空值,则

request_path like '%Portal%'
应该是

(request_path not like '%Portal%' or request_path is null)

因为
null不象'anything'
将返回null。

您没有反转
类别id不为null
产品id不为null
。使其
类别\u id不为空或产品\u id不为空
。它就在那里。-但是您也可以简单地使用
where not(初始条件)
但是有一个问题:如果
request\u path
null
的行会发生什么
null
值既不像
中的“某物”
也不像
中的“某物”
。请参阅