where子句中的NULL值 [英] NULL values in where clause

查看:221
本文介绍了where子句中的NULL值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的表"bla":

i've got a table "bla" like this:

[id]    [name]    [fk]
1       test      4
2       foo       5
3       bar       NULL

如果我执行sql查询

SELECT * FROM bla WHERE fk <> 4

我只获得ID为2的记录.我没有获得ID为3的记录,其中fk为空. 我以为NULL!=4.看来这是错误的.

i only get the record with the id 2. i don't get the record with id 3 where fk is null. I thought NULL != 4. Seems that this is wrong.

为什么会这样?

推荐答案

NULL不等于任何东西.您需要明确接受null:

NULL doesn't compare equal to anything. You'll need to accept nulls explicitly:

where fk <> 4 or fk is null;

有关更多信息,请参见使用NULL 有关NULL处理的信息.

See Working with NULL for more information about NULL handling.

这篇关于where子句中的NULL值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆