MySQL 比较运算符,空格

标签 mysql comparison-operators

如果数据库行是这样的: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/

相关文章:

c++ - 比较成员指针

r - R中的数值比较困难

c++ - 在条件之外使用比较运算符

c++ - 在没有大条件 block 的情况下更改比较运算符

mysql - 加入mysql有什么更好的方法?

mysql - 存储过程 SQL 语法 : error missing > 'end'

mysql - 按天分组计算总和

php - 仅当有空位时才选择事件

Mysql - 选择到 XML

javascript - 使用 == 比较整数与字符串时 JavaScript 中的隐式数据类型转换