gpt4 book ai didi

MySQL 比较运算符,空格

转载 作者:可可西里 更新时间:2023-11-01 06:28:53 24 4
gpt4 key购买 nike

如果数据库行是这样的:country = 'usa' 并且我查询 "select * from data where country = 'usa'" 它也返回这一行.所以它不是完全匹配。

为什么 MySQL 这样做?在其他情况下,当它不是真的时,它也会返回 TRUE?

最佳答案

the manual 中所述:

All MySQL collations are of type PADSPACE. This means that all CHAR and VARCHAR values in MySQL are compared without regard to any trailing spaces.

LIKE的定义中运营商,它指出:

In particular, trailing spaces are significant, which is not true for CHAR or VARCHAR comparisons performed with the = operator:

this answer 中所述:

This behavior is specified in SQL-92 and SQL:2008. For the purposes of comparison, the shorter string is padded to the length of the longer string.

From the draft (8.2 <comparison predicate>):

If the length in characters of X is not equal to the length in characters of Y, then the shorter string is effectively replaced, for the purposes of comparison, with a copy of itself that has been extended to the length of the longer string by concatenation on the right of one or more pad characters, where the pad character is chosen based on CS. If CS has the NO PAD characteristic, then the pad character is an implementation-dependent character different from any character in the character set of X and Y that collates less than any string under CS. Otherwise, the pad character is a <space>.

除了其他优秀的解决方案:

select binary 'a' = 'a   '

关于MySQL 比较运算符,空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10495692/

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