计数 (*) 不适用于 where 子句
我尝试编写的相当基本的查询有问题。
select count (p_id) as mycustomer from person where date_active = null;
这是行不通的。 (运行,但返回 0)但是,下面的语句确实(并返回一个数字),有人可以帮忙吗?
select count (p_id) as mycustomer from person wher date_active > '1-MAY-09';
我试图查找表中 date_active 为空的 p_ids 总数。 (它们确实存在,我可以看到它们!) 谢谢
have an issue with a fairly basic query I'm attempting to write.
select count (p_id) as mycustomer from person where date_active = null;
Which doesn't work. (runs, but returns 0) However, the below statement does (and returns a number), can anyone help?
select count (p_id) as mycustomer from person wher date_active > '1-MAY-09';
I am trying to find the total number of p_ids in a table that have a date_active as null. (They do exist, I can see them!)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为你需要使用:
I think you need to use:
尝试以下
= null
是不正确的,不会执行您想要的操作。此链接提供了更多详细信息Try the following
= null
is incorrect and won't do what you want. This link gives more detail您必须使用
is
运算符来查找 null:NULL 不是一个值(它表示不存在 vlaue),因此您不能使用 =、!=、<、 >等与 NULL 结合使用。
You must use the
is
oprator to find null:NULL isn't a value (it indicates the absense of a vlaue), so you can't use scalar oprators like =, !=, <, > etc. in conjunction with NULL.
这应该有效:
this should work:
(注意 IS NULL 运算符而不是=)
(Note the IS NULL operator instead of =)
SQL 不支持二进制逻辑(仅
true
和false
)。它(至少)适用于三元逻辑(true
、false
和null
)。第三个值使逻辑条件的评估变得复杂(例如条件中的=
)。基本上,任何涉及 null 的表达式都将返回 null 值,并且由于
null
永远不会true
,因此查询不会返回任何行,就像您编写的那样为了解决这个问题,有一些特殊的语法结构,最常见的是
is null
和is not null
条件。因此,正如其他答案所指出的,要获取空值,您需要编写关于空值的有趣的事情是它们既不等于也不等于其他空值。
并且
两者都不会返回任何行。
SQL does not work with binary logic (only
true
andfalse
). It works with (at least) ternary logic (true
,false
andnull
). That third value complicates the evaluation of logical conditions (like the=
you have in the condition).Basically, any expression that involves a null, will return a null value, and since
null
is nevertrue
, the query won't return any rows, just like if you wroteTo work around this there are some special syntax constructs, most common being the
is null
andis not null
conditions. So as the other answers pointed out, to get the null values you need to writeInteresting thing about nulls is that they are neither equal not inequal to other nulls.
and
both won't return any rows.