MySQL: selecting rows where a column is null(MySQL:选择列为空的行)
问题描述
我遇到了一个问题,当我尝试为某个列选择具有 NULL 的行时,它返回一个空集.但是,当我查看 phpMyAdmin 中的表格时,大多数行都显示为 null.
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.
我的查询如下所示:
SELECT pid FROM planets WHERE userid = NULL
每次都是空集.
很多地方说要确保它不会存储为NULL"或null"而不是实际值,还有一个说尝试只查找一个空格 (userid = ' '
) 但这些都没有奏效.有人建议不要使用 MyISAM 而使用 innoDB,因为 MyISAM 无法存储 null.我将表切换到 innoDB,但现在我觉得问题可能在于它实际上仍然不是 null,因为它可能转换它的方式.我想这样做而不必将表重新创建为 innoDB 或其他任何东西,但如果必须,我当然可以尝试.
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.
推荐答案
SQL NULL 的特殊性,你必须做 WHERE field IS NULL
,因为 NULL 不能等于任何东西,
SQL NULL's special, and you have to do WHERE field IS NULL
, as NULL cannot be equal to anything,
包括自身(即:NULL = NULL 总是假的).
参见规则 3
https://en.wikipedia.org/wiki/Codd%27s_12_rules
这篇关于MySQL:选择列为空的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL:选择列为空的行


- 在SQL中,如何为每个组选择前2行 2021-01-01
- SQL 临时表问题 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01