MySQL:选择列为空的行

发布于 2024-09-15 13:12:11 字数 433 浏览 2 评论 0原文

我遇到一个问题,当我尝试选择某个列具有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

一花一树开 2024-09-22 13:12:11

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

意中人 2024-09-22 13:12:11
SELECT pid FROM planets WHERE userid IS NULL
SELECT pid FROM planets WHERE userid IS NULL
假情假意假温柔 2024-09-22 13:12:11

既然所有人都给出了答案,我想补充一点。我也遇到过同样的问题。

为什么你的查询失败了?你有,

SELECT pid FROM planets WHERE userid = NULL;

这不会给你预期的结果,因为来自 mysql文档

在 SQL 中,与任何其他值(甚至 NULL)相比,NULL 值永远不会为 true。包含 NULL 的表达式始终生成 NULL 值,除非运算符和函数的文档中另有说明参与表达。

强调我的。

要搜索 NULL 列值,不能使用 expr = NULL 测试。以下语句不返回任何行,因为 expr = NULL 对于任何表达式都不会为 true

解决方案

SELECT pid FROM planets WHERE userid IS NULL; 

要测试 NULL,请使用 IS NULLIS NOT 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,

SELECT pid FROM planets WHERE userid = NULL;

This will not give you the expected result, because from mysql doc

In SQL, the NULL value is never true in comparison to any other value, even NULL. An expression that contains NULL always produces a NULL value unless otherwise indicated in the documentation for the operators and functions involved in the expression.

Emphasis mine.

To search for column values that are NULL, you cannot use an expr = NULL test. The following statement returns no rows, because expr = NULL is never true for any expression

Solution

SELECT pid FROM planets WHERE userid IS NULL; 

To test for NULL, use the IS NULL and IS NOT NULL operators.

晒暮凉 2024-09-22 13:12:11

还有一个 <=> 运算符:

SELECT pid FROM planets WHERE userid <=> 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:

SELECT pid FROM planets WHERE userid <=> NULL

Would work. The nice thing is that <=> can also be used with non-NULL values:

SELECT NULL <=> NULL yields 1.

SELECT 42 <=> 42 yields 1 as well.

See here: https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal-to

っ左 2024-09-22 13:12:11

来自 http://w3schools.com/sql/sql_null_values.asp 的信息:

1) NULL 值表示缺失的未知数据。

2) 默认情况下,表列可以保存 NULL 值。

3) NULL 值与其他值的处理方式不同

4) 不能比较NULL和0;它们并不等同。

5) 无法通过比较来测试 NULL 值
运算符,例如 =、< 或 <>。

6) 我们将不得不使用 IS NULL 和 IS NOT NULL 运算符

所以如果出现问题:

SELECT pid FROM planets WHERE userid IS NULL

Info from http://w3schools.com/sql/sql_null_values.asp:

1) NULL values represent missing unknown data.

2) By default, a table column can hold NULL values.

3) NULL values are treated differently from other values

4) It is not possible to compare NULL and 0; they are not equivalent.

5) It is not possible to test for NULL values with comparison
operators, such as =, <, or <>.

6) We will have to use the IS NULL and IS NOT NULL operators instead

So in case of your problem:

SELECT pid FROM planets WHERE userid IS NULL
原谅我要高飞 2024-09-22 13:12:11
SELECT pid FROM planets WHERE userid is null;
SELECT pid FROM planets WHERE userid is null;
鹿童谣 2024-09-22 13:12:11

遇到同样的问题,查询:

SELECT * FROM 'column' WHERE 'column' IS NULL; 

未返回任何值。
似乎是 MyISAM 的问题,对 InnoDB 中的数据进行相同的查询返回了预期的结果。

一起去:

SELECT * FROM 'column' WHERE 'column' = ' '; 

返回所有预期结果。

Had the same issue where query:

SELECT * FROM 'column' WHERE 'column' IS NULL; 

returned no values.
Seems to be an issue with MyISAM and the same query on the data in InnoDB returned expected results.

Went with:

SELECT * FROM 'column' WHERE 'column' = ' '; 

Returned all expected results.

慵挽 2024-09-22 13:12:11

在将数据库从 Access 转换为 MySQL 时(使用 vb.net 与数据库通信),我遇到了同样的问题。

我需要评估字段(字段类型 varchar(1))是否为空。

这个声明适用于我的场景:

SELECT * FROM [table name] WHERE [field name] = ''

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:

SELECT * FROM [table name] WHERE [field name] = ''
薄荷港 2024-09-22 13:12:11

将代码替换为这个
它将工作

从行星中选择pid,其中userid为NULL

Replace the code with this one
it will work

SELECT pid FROM planets WHERE userid IS NULL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文