背景
查询数据时,值带着空格也可以被查到,经查询,发现mysql varchar字段自动忽略尾部空格,特此记录
正文
参考该地址
MySQL comparison operator, spaces - Stack Overflow
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.
只要是varchar或者是char字段,末尾空格会被忽略
解决方案
1. 使用like 替换=运算符
where name='123'--> where name like '123'
2. 使用关键字 binary
where binary name='123'
3. 使用CHAR_LENGTH
CHAR_LENGTH(name)=CHAR_LENGTH('be ')
总结
经过实践,第一种较为符合本人所遇问题