mysql - 当条件之一为空时,Mysql xor 运算符无法正常工作
问题描述
我需要为 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
为假,因此整体条件也为假。
推荐阅读
- c# - 如何使用 C# 通过他的 ID 获取当前登录的用户?
- html - 如何在 Angular 的对话框中显示输入更改?
- javascript - 如何检索包含特定字符串的特定对象
- autocad - 运行 AutoLISP 文件的脚本
- linux - 当accept() 失败且不是EAGAIN 或EWOULDBLOCK(非阻塞IO)时该怎么办?
- tensorflow - 经过一段时间的训练,训练损失值在增加,但模型检测到的对象很好
- c# - ASP.net 核心 RegularException 属性 - 多个条件
- c# - 为什么我不能在构造函数中设置这个只读属性?
- javascript - 使用 lodash 对翻转函数进行柯里化 - 有限制吗?
- coq - 在 Coq 中匹配假设的更短的符号?