MySQL查询条件中字符串包含空格的问题
2013-11-27 13:43
591 查看
2013-11-27 wcdj
方法1:使用like语句;
方法2:使用binary类型,例如,select binary 'a' = 'a '
方法3:再添加一个length()条件,例如,select col from table where col = 'a ' and LENGTH(col) = LENGTH('a ')
官方手册说明(5.0版本):
http://dev.mysql.com/doc/refman/5.0/en/char.html
11.1.6.1. The
All MySQL collations are of type
to any trailing spaces. “Comparison” in this context does not include the
operator, for which trailing spaces are significant.
在stackoverfolw上也可以找到类似的问题:
MySQL comparison operator, spaces
http://stackoverflow.com/questions/10495692/mysql-comparison-operator-spaces
问题
最近在联调某个业务时发现使用的签名总是验证不过,在MySQL中查询了该业务的私钥配置和业务侧的配置是一样的,问题就出在SQL查询这里,最后将配置导出到本地发现私钥后面多了一个空格,将空格删除然后签名计算OK。问题是:为什么在DB查询条件中的字符串没有包含空格也可以查到实际包含空格的这条记录呢?原因
如果字段是char或varchar类型,那么在字符串比较的时候MySQL使用PADSPACE校对规则,会忽略字段末尾的空格字符,若想做到精确匹配可以使用下面几种方法:方法1:使用like语句;
方法2:使用binary类型,例如,select binary 'a' = 'a '
方法3:再添加一个length()条件,例如,select col from table where col = 'a ' and LENGTH(col) = LENGTH('a ')
官方手册说明(5.0版本):
http://dev.mysql.com/doc/refman/5.0/en/char.html
11.1.6.1. The
CHARand
VARCHARTypes
All MySQL collations are of type
PADSPACE. This means that all
CHAR,
VARCHAR, and
TEXTvalues in MySQL are compared without regard
to any trailing spaces. “Comparison” in this context does not include the
LIKEpattern-matching
operator, for which trailing spaces are significant.
在stackoverfolw上也可以找到类似的问题:
MySQL comparison operator, spaces
http://stackoverflow.com/questions/10495692/mysql-comparison-operator-spaces
相关文章推荐
- MySQL中关于查询条件中的字符串空格问题
- MySQL中关于查询条件中的字符串空格问题
- MySQL中关于查询条件中的字符串空格问题
- MySQL查询时的字符串空格问题
- 关于Mysql查询带单引号及插入带单引号字符串问题
- Mysql中使用FIND_IN_SET解决IN条件为字符串时只有第一个数据可用的问题
- MySQL种字符串拆分(substring_index)并作为关联查询条件后多列展示
- MySqlCommand查询条件中包含中文没有结果的问题
- MySQL种字符串拆分(substring_index)并作为关联查询条件后多列展示
- mysql判断查询的字段的数据是否包含指定的字符串
- mysql查找语句中包含中文字符时查询失效的问题及解决方法
- MySQL 中文显示乱码以及中文查询条件返回0条结果的问题解决
- MySQL种字符串拆分(substring_index)并作为关联查询条件后多列展示
- MYBATIS 根据IN条件查询时,数据只查第一个的问题(字符串被截断......)
- MYSQL中利用select查询某字段中包含以逗号分隔的字符串的记录方法
- MYSQL中利用select查询某字段中包含以逗号分隔的字符串的记录方法
- mysql查询条件失效问题
- mysql分组查询结果集作为条件查询的问题
- MYSQL中利用select查询某字段中包含以逗号分隔的字符串的记录方法
- MySQL种字符串拆分(substring_index)并作为关联查询条件后多列展示