SQL 为空且 = 空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9581745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:41:21  来源:igfitidea点击:

SQL is null and = null

sqlnull

提问by Sachin Kainth

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

可能的重复:
什么是“=null”和“IS NULL”
IS NULL 和 =NULL 之间有什么区别

What is the difference between

之间有什么区别

where x is null

and

where x = null

and why does the latter not work?

为什么后者不起作用?

回答by Bohemian

In SQL, a comparison between a nullvalue and any other value (including another null) using a comparison operator (eg =, !=, <, etc) will result in a null, which is considered as falsefor the purposes of a where clause (strictly speaking, it's "not true", rather than "false", but the effect is the same).

在SQL中,之间的比较null值,任何其他值(包括另一种null使用比较操作符(如)=!=<,等)将导致null,这被认为是false一个where子句的目的(严格来说,它的“不true”,而不是“false”,但效果是一样的)。

The reasoning is that a nullmeans "unknown", so the result of any comparison to a nullis also "unknown". So you'll get no hit on rows by coding where my_column = null.

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

SQL provides the special syntax for testing if a column is null, via is nulland is not null, which is a special condition to test for a null(or not a null).

SQL 提供了用于测试列是否为null、通过is null和的特殊语法is not null,这是测试 a null(或不是 a null)的特殊条件。

Here's some SQL showing a variety of conditions and and their effect as per above.

这里有一些 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);

returns only 1 row (as expected):

仅返回 1 行(如预期):

TEST    X   Y
x = y   1   1

See this running on SQLFiddle

看到这个在SQLFiddle 上运行

回答by Curt

It's important to note, that NULL doesn't equal NULL.

需要注意的是,NULL 不等于 NULL

NULLis not a value, and therefore cannot be compared to another value.

NULL不是一个值,因此不能与另一个值进行比较。

where x is nullchecks whether x is a null value.

where x is null检查 x 是否为空值。

where x = nullis checking whether x equals NULL, which will never be true

where x = null正在检查 x 是否等于 NULL,这永远不会是真的

回答by Sarfraz

First is correct way of checking whether a field value is nullwhile later won't work the way you expect it to because nullis special value which does not equal anything, so you can't use equality comparison using =for it.

首先是检查字段值是否正确的方法,null而稍后将无法按照您期望的方式工作,因为null它是不等于任何值的特殊值,因此您不能使用=它来进行相等比较。

So when you need to check if a field value is nullor not, use:

因此,当您需要检查字段值是否null存在时,请使用:

where x is null

instead of:

代替:

where x = null

回答by Vikram

I think that equality is something that can be absolutely determined. The trouble with nullis that it's inherently unknown. Nullcombined with any other value is null- unknown. Asking SQL "Is my value equal to null?" would be unknown every single time, even if the input is null. I think the implementation of IS NULLmakes it clear.

我认为平等是可以绝对确定的。问题null在于它本质上是未知的。Null结合任何其他值是null- 未知。询问 SQL “我的值等于null吗?” 即使输入是null. 我认为 的实施已经IS NULL很清楚了。