谁能解释一下这两个“空字符串”有什么区别?

发布于 2025-01-11 13:47:11 字数 1549 浏览 0 评论 0原文

因此,我有一个表,其中 ENUM 列的默认值为 NULL。 我做了一些测试,将一些行设置为各种值,然后将其中一行更新为不在 ENUM 中的值。然后我得到了这个: MariaDB [my_db]> select this_value, count(*) FROM my_table GROUP by this_value;

+--------------------+----------+
| this_value         | count(*) |
+--------------------+----------+
| NULL               |      242 |
|                    |        1 |
| AA                 |        1 |
| AB                 |        1 |
| AC                 |        1 |
| BA                 |        1 |
| BB                 |        1 |
| BC                 |        1 |
+--------------------+----------+
8 rows in set (0.001 sec)

然后,我执行了 ALTER TABLE my_table MODIFY COLUMN this_value,将 '' 添加到值选项列表中并设为默认值 ''。

现在,这就是您得到的结果:

MariaDB [my_db]> select this_value, count(*) FROM my_table GROUP by this_value;
+--------------------+----------+
| this_value         | count(*) |
+--------------------+----------+
|                    |        1 |
|                    |      242 |
| AA                 |        1 |
| AB                 |        1 |
| AC                 |        1 |
| BA                 |        1 |
| BB                 |        1 |
| BC                 |        1 |
+--------------------+----------+
8 rows in set (0.001 sec)

那么,两个“空字符串”之间有什么区别。这样做的整个目标是因为使用像 this_value NOT LIKE 'A%' 这样的查询,我们没有得到 NULL 值(我们想要那些,因为它们“不是以 A”值开头),这确实解决了这个问题(“SELECT count(*) FROM my_table WHERE this_value NOT LIKE "A%" returns 246),但是,我只是对Mysql如何区分这两个值感兴趣......对其执行 CHAR_LENGTH 显示它们都是 0,但显然它们仍然不完全“等于”mysql。

So, I had a table where the default was NULL on an ENUM column.
I did some testing, setting some rows to various values, and then updated one of the rows to a value not in the ENUM. I then got this:
MariaDB [my_db]> select this_value, count(*) FROM my_table GROUP by this_value;

+--------------------+----------+
| this_value         | count(*) |
+--------------------+----------+
| NULL               |      242 |
|                    |        1 |
| AA                 |        1 |
| AB                 |        1 |
| AC                 |        1 |
| BA                 |        1 |
| BB                 |        1 |
| BC                 |        1 |
+--------------------+----------+
8 rows in set (0.001 sec)

Then, I did a ALTER TABLE my_table MODIFY COLUMN this_value, adding the '' to the list of value options and making the default ''.

Now, this is what you get:

MariaDB [my_db]> select this_value, count(*) FROM my_table GROUP by this_value;
+--------------------+----------+
| this_value         | count(*) |
+--------------------+----------+
|                    |        1 |
|                    |      242 |
| AA                 |        1 |
| AB                 |        1 |
| AC                 |        1 |
| BA                 |        1 |
| BB                 |        1 |
| BC                 |        1 |
+--------------------+----------+
8 rows in set (0.001 sec)

So, what is the difference between the two "empty strings". The whole goal of this was because with a query like this_value NOT LIKE 'A%', we were not getting the NULL values (and we wanted those, since they were "not starting with A" values), and this does resolve to that ("SELECT count(*) FROM my_table WHERE this_value NOT LIKE "A%" returns 246), but, I am just interested in how Mysql is differentiating those two values... doing a CHAR_LENGTH on it shows they are both 0, but obviously they still aren't exactly "equivalent" to mysql.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文