“从表中删除 NULL = NULL”是什么意思?方法?

发布于 2024-09-08 22:59:07 字数 62 浏览 6 评论 0原文

delete from table where NULL = NULL 是什么意思?

What does delete from table where NULL = NULL mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

笑叹一世浮沉 2024-09-15 22:59:08

这意味着不要删除任何内容,因为 NULL 永远不等于任何内容。或者它的意思可能是“不要删除任何东西,除非用户的 DBMS 真的很糟糕,在这种情况下,出于恶意将其全部删除”。

但说真的,这种构造通常在程序生成 WHERE 子句时出现 - 而不是创建“不执行任何操作”的特殊情况,有时生成一个导致数据库不执行任何操作的 WHERE 子句会更简单。不过,我通常看到“WHERE 0 = 1”,这不太含糊。

It means don't delete anything, because NULL is never equal to anything. Or maybe it means "don't delete anything unless the user's DBMS really sucks, in which case delete it all out of spite".

Seriously though, that kind of construct usually comes about when a WHERE clause is procedurally generated -- rather than creating a special case for "do nothing", sometimes it's simpler just to generate a WHERE clause that causes the database to do nothing. I've usually seen "WHERE 0 = 1" though, which is less ambiguous.

于我来说 2024-09-15 22:59:08

在SQL中,有3个逻辑值,分别是TRUE、FALSE和UNKNOWN。当我们将 null 与 null 进行比较时,使用 null=null,该操作将返回 UNKNOWN。此外,在 WHERE 子句中,所有 UNKNOWN 值都被过滤掉。因此查询不执行任何操作。

In SQL, there are three logical values, namely TRUE, FALSE, and UNKNOWN. when we compare null to null, using null=null, the operation will return UNKNOWN. Moreover,In the WHERE clause all UNKNOWN values are filtered out.Hence the query does nothing.

迷爱 2024-09-15 22:59:08

它惩罚那些在数据库中将 ANSI_NULLS 设置为关闭的人:)

It punishes people who have ANSI_NULLS set to off in their database :)

如痴如狂 2024-09-15 22:59:08

由于 NULL 不等于 NULL,因此该语句不会执行任何操作。它等于:

DELETE FROM TABLE WHERE 0

Since NULL does not equal NULL, this statement will do nothing. It equals:

DELETE FROM TABLE WHERE 0
不…忘初心 2024-09-15 22:59:08

概括一下:

mysql> select null = null, null <> null, null is null, null = 1, null <> 1;
+-------------+--------------+--------------+----------+-----------+
| null = null | null <> null | null is null | null = 1 | null <> 1 |
+-------------+--------------+--------------+----------+-----------+
|        NULL |         NULL |            1 |     NULL |      NULL |
+-------------+--------------+--------------+----------+-----------+
1 row in set (0.00 sec)


mysql> select count(*) from table;
+----------+
| count(*) |
+----------+
|     10   |
+----------+
1 row in set (0.33 sec)


mysql> select * from table where null;
Empty set (0.00 sec)

意思是,如果条件计算结果为 null,则 MySql 会认为该条件为 false,因此从 NULL = NULL 的表中删除实际上不会删除任何内容。

Codd 指出,NULL 是一种特殊的野兽

Recap:

mysql> select null = null, null <> null, null is null, null = 1, null <> 1;
+-------------+--------------+--------------+----------+-----------+
| null = null | null <> null | null is null | null = 1 | null <> 1 |
+-------------+--------------+--------------+----------+-----------+
|        NULL |         NULL |            1 |     NULL |      NULL |
+-------------+--------------+--------------+----------+-----------+
1 row in set (0.00 sec)


mysql> select count(*) from table;
+----------+
| count(*) |
+----------+
|     10   |
+----------+
1 row in set (0.33 sec)


mysql> select * from table where null;
Empty set (0.00 sec)

Meaning, if a condition evaluates to null it is considered false by MySql so delete from table where NULL = NULL will in fact delete nothing.

NULL is a special beast as noted by Codd

苏璃陌 2024-09-15 22:59:08

我想这取决于数据库,但据我所知,它不应该实现任何目标,因为 NULL 永远不等于 NULL,至少在数据库理论中是这样。

I imagine it depends on the database, but to my knowledge, it shouldn't achieve anything, as NULL is never equal to NULL, at least in db theory.

忆伤 2024-09-15 22:59:08

此 SQL 命令不会影响任何行。

Not a single row is affected by this SQL command.

看透却不说透 2024-09-15 22:59:07

这不会从表中删除任何内容。
NULL 不等于 NULL。

现在

从表中删除NULL为NULL的地方

将删除表中的所有行。

That will delete nothing from the table.
NULL does not equal NULL.

Now

delete from table where NULL is NULL

would delete all rows from the table.

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