gpt4 book ai didi

SQL为空且=空

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

Possible Duplicate:
what is “=null” and “ IS NULL”
Is there any difference between IS NULL and =NULL

有什么区别

where x is null

where x = null

为什么后者不起作用?

最佳答案

在 SQL 中,null 之间的比较值和任何其他值(包括另一个 null )使用比较运算符(例如 =!=< 等)将产生 null ,被视为 false出于 where 子句的目的(严格来说,它是“not true”,而不是“false”,但效果是相同的)。

推理是 null表示“未知”,因此与 null 进行任何比较的结果也是“未知”。因此,通过编码 where my_column = null 不会对行产生任何影响。 .

SQL 提供了特殊语法来测试列是否为 null ,通过is nullis not null ,这是测试 null 的特殊条件。 (或者不是 null )。

这里有一些 SQL 显示了上面的各种条件及其效果。

create table t (x int, y int);
insert into t values (null, null), (null, 1), (1, 1);

select 'x = null' as test , x, y from t where x = null
union all
select 'x != null', x, y from t where x != null
union all
select 'not (x = null)', x, y from t where not (x = null)
union all
select 'x = y', x, y from t where x = y
union all
select 'not (x = y)', x, y from t where not (x = y);

仅返回 1 行(如预期):

TEST    X   Y
x = y 1 1

查看此运行于 SQLFiddle

关于SQL为空且=空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9581745/

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