gpt4 book ai didi

mysql-5.7 - mysql,空格等于空字符串

转载 作者:行者123 更新时间:2023-12-04 04:10:28 25 4
gpt4 key购买 nike

我只花了 2 个小时就解决了后端的问题。

原因是空字符串等于空格:

SELECT ' ' = '';
-> 1

SELECT STRCMP(' ', '');
-> 0 /* means equal */

有趣的是,
SELECT '' REGEXP '[ ]';
-> 0
SELECT '' REGEXP ' ';
-> 0
SELECT ' ' REGEXP ' ';
-> 1

我可以防止这种情况吗?是设定吗?

最佳答案

此处的文档中解释了失败的原因 http://dev.mysql.com/doc/refman/5.0/en/char.html :

Values in CHAR and VARCHAR columns are sorted and compared according to the character set collation assigned to the column.

All MySQL collations are of type PADSPACE. This means that all CHAR, VARCHAR, and TEXT values in MySQL are compared without regard to any trailing spaces. “Comparison” in this context does not include the LIKE pattern-matching operator, for which trailing spaces are significant.



解决此问题的一种方法是强制转换为 BINARY
SELECT BINARY '' = ' ';
0

您也可以使用 LIKE :
SELECT '' LIKE ' ';
0

关于mysql-5.7 - mysql,空格等于空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34782594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com