gpt4 book ai didi

sql - 按位或-SQLite中的意外结果

转载 作者:行者123 更新时间:2023-12-03 18:41:39 25 4
gpt4 key购买 nike

最近,我们在生成SQLite查询时遇到了一个问题,我们意外地使用了按位OR:

SELECT * FROM demo WHERE ((nullableId IS NULL) | (nullableId = -1))


我们使用 OR运算符解决了此问题。

但是上面的查询有一些我不理解的意外行为:


为什么这甚至有效? (例如,在Microsoft SQL Server中,这显然是语法错误)
为什么结果所有带有 nullableId = -1的行?
简而言之,这是怎么回事?

最佳答案

nullableId IS NULL时,条件评估:
nullableId IS NULL至1
nullableId = -1NULL(因为与NULL的任何比较都会返回NULL
所以这个表达式:

(nullableId IS NULL) | (nullableId = -1)


计算为 NULL,并且由于t不是 TRUE,因此不返回这些行。
但是,当 nullableId = -1时,表达式的计算结果为:

0 | 1 = 1


因此,返回所有带有 nullableId = -1的行。
您可以在如下表中看到此行为:

create table demo(nullableId int);
insert into demo(nullableId) values
(1), (null), (-1), (2);


查询:

SELECT *, 
(nullableId IS NULL),
(nullableId = -1),
(nullableId IS NULL) | (nullableId = -1)
FROM demo


返回:

| nullableId | (nullableId IS NULL) | (nullableId = -1) | (nullableId IS NULL) | (nullableId = -1) |
| ---------- | -------------------- | ----------------- | ---------------------------------------- |
| 1 | 0 | 0 | 0 |
| null | 1 | null | null |
| -1 | 0 | 1 | 1 |
| 2 | 0 | 0 | 0 |

关于sql - 按位或-SQLite中的意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59157520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com