谁能解释一下这两个“空字符串”有什么区别?
因此,我有一个表,其中 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论