Mysql 当其中一个条件为 null 时,xor 运算符无法正常工作
Mysql xor operator does not properly work, when one of condition is null
我需要为 select 添加一个子句,其中记录等于值或等于 null。
select *
from table
where column = 123 or column is null;
结果我得到
|column|
123
123
null
null
当我尝试使用 xor
select *
from table
where column = 123 xor column is null;
我要么只有 123 条记录
|column|
123
123
或者,如果没有123,我什么都没有
|列|
但是我需要,当没有123条记录时只获取空值。所以基本上我想要的结果是:
|db column| |column with 123 clause| |column with 125 clause|
123 123 null
123 123 null
null
null
124
你能帮我解决这个问题吗?
如果第一行和第二行不返回,你希望返回第三行和第四行,对吗?这不能用简单的 where
子句来表达。尝试
select * from table where column=123
or column is null and not exists
(select * from table where column=123)
关于您的 xor
观察:我假设
column=123 xor column is null
等同于
column=123 and column is not null or
column<>123 and column is null
但是当column = null
时,表达式column<>123
为假,因此整体条件也为假。
我需要为 select 添加一个子句,其中记录等于值或等于 null。
select *
from table
where column = 123 or column is null;
结果我得到
|column|
123
123
null
null
当我尝试使用 xor
select *
from table
where column = 123 xor column is null;
我要么只有 123 条记录
|column|
123
123
或者,如果没有123,我什么都没有
|列|
但是我需要,当没有123条记录时只获取空值。所以基本上我想要的结果是:
|db column| |column with 123 clause| |column with 125 clause|
123 123 null
123 123 null
null
null
124
你能帮我解决这个问题吗?
如果第一行和第二行不返回,你希望返回第三行和第四行,对吗?这不能用简单的 where
子句来表达。尝试
select * from table where column=123
or column is null and not exists
(select * from table where column=123)
关于您的 xor
观察:我假设
column=123 xor column is null
等同于
column=123 and column is not null or
column<>123 and column is null
但是当column = null
时,表达式column<>123
为假,因此整体条件也为假。