MySQL:选择列为空的行
我遇到一个问题,当我尝试选择某个列具有 NULL 的行时,它返回一个空集。但是,当我查看 phpMyAdmin 中的表时,大多数行都显示为 null。
我的查询看起来像这样:
SELECT pid FROM planets WHERE userid = NULL
每次都是空集。
很多地方都说要确保它不会存储为“NULL”或“null”而不是实际值,还有一个地方说尝试仅查找空格(userid = ' '
),但没有其中一些已经奏效。有人建议不要使用MyISAM而使用innoDB,因为MyISAM在存储null时遇到麻烦。我将表切换到 innoDB,但现在我觉得问题可能是它实际上仍然不是 null,因为它可能转换它的方式。我想这样做,而不必将表重新创建为 innoDB 或其他任何东西,但如果必须的话,我当然可以尝试这样做。
I'm having a problem where when I try to select the rows that have a NULL for a certain column, it returns an empty set. However, when I look at the table in phpMyAdmin, it says null for most of the rows.
My query looks something like this:
SELECT pid FROM planets WHERE userid = NULL
Empty set every time.
A lot of places said to make sure it's not stored as "NULL" or "null" instead of an actual value, and one said to try looking for just a space (userid = ' '
) but none of these have worked. There was a suggestion to not use MyISAM and use innoDB because MyISAM has trouble storing null. I switched the table to innoDB but now I feel like the problem may be that it still isn't actually null because of the way it might convert it. I'd like to do this without having to recreate the table as innoDB or anything else, but if I have to, I can certainly try that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
SQL NULL 很特殊,您必须执行
WHERE field IS NULL
,因为 NULL 不能等于任何内容,包括其自身(即:NULL = NULL 始终为 false)。请参阅
规则 3
https://en.wikipedia.org/wiki/Codd% 27s_12_规则SQL NULL's special, and you have to do
WHERE field IS NULL
, as NULL cannot be equal to anything,including itself (ie: NULL = NULL is always false).See
Rule 3
https://en.wikipedia.org/wiki/Codd%27s_12_rules既然所有人都给出了答案,我想补充一点。我也遇到过同样的问题。
为什么你的查询失败了?你有,
这不会给你预期的结果,因为来自 mysql文档
强调我的。
解决方案
要测试
NULL
,请使用IS NULL
和IS NOT NULL
运算符。NULL
。NULL
。As all are given answers I want to add little more. I had also faced the same issue.
Why did your query fail? You have,
This will not give you the expected result, because from mysql doc
Emphasis mine.
Solution
To test for
NULL
, use theIS NULL
andIS NOT NULL
operators.NULL
.NULL
.还有一个
<=>
运算符:可以工作。好处是
<=>
也可以与非 NULL 值一起使用:SELECT NULL <=>; NULL
产生1
。选择 42 <=> 42
也会产生1
。请参阅此处:https://dev.mysql。 com/doc/refman/5.7/en/comparison-operators.html#operator_equal-to
There's also a
<=>
operator:Would work. The nice thing is that
<=>
can also be used with non-NULL values:SELECT NULL <=> NULL
yields1
.SELECT 42 <=> 42
yields1
as well.See here: https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal-to
来自 http://w3schools.com/sql/sql_null_values.asp 的信息:
所以如果出现问题:
Info from http://w3schools.com/sql/sql_null_values.asp:
So in case of your problem:
遇到同样的问题,查询:
未返回任何值。
似乎是 MyISAM 的问题,对 InnoDB 中的数据进行相同的查询返回了预期的结果。
一起去:
返回所有预期结果。
Had the same issue where query:
returned no values.
Seems to be an issue with MyISAM and the same query on the data in InnoDB returned expected results.
Went with:
Returned all expected results.
在将数据库从 Access 转换为 MySQL 时(使用 vb.net 与数据库通信),我遇到了同样的问题。
我需要评估字段(字段类型 varchar(1))是否为空。
这个声明适用于我的场景:
I had the same issue when converting databases from Access to MySQL (using vb.net to communicate with the database).
I needed to assess if a field (field type varchar(1)) was null.
This statement worked for my scenario:
将代码替换为这个
它将工作
从行星中选择pid,其中userid为NULL
Replace the code with this one
it will work
SELECT pid FROM planets WHERE userid IS NULL