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
SQL is null and = null
提问by Sachin Kainth
Possible Duplicate:
what is “=null” and “ IS NULL”
Is there any difference between IS NULL and =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 null
value and any other value (including another null
) using a comparison operator (eg =
, !=
, <
, etc) will result in a null
, which is considered as false
for 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 null
means "unknown", so the result of any comparison to a null
is 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 null
and 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。
NULL
is not a value, and therefore cannot be compared to another value.
NULL
不是一个值,因此不能与另一个值进行比较。
where x is null
checks whether x is a null value.
where x is null
检查 x 是否为空值。
where x = null
is 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 null
while later won't work the way you expect it to because null
is 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 null
or 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 null
is that it's inherently unknown. Null
combined 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 NULL
makes it clear.
我认为平等是可以绝对确定的。问题null
在于它本质上是未知的。Null
结合任何其他值是null
- 未知。询问 SQL “我的值等于null
吗?” 即使输入是null
. 我认为 的实施已经IS NULL
很清楚了。