mysql 查询参数尾部有空格时被忽略

  最近再使用mysql时,无意见发现,当我查询参数尾部输入若干个空格时,依然可以查出和不加空格相同的结果,像这样

   select * from wa where name='be   '
     等同于
   select * from wa where name='be';

  查询大量资(bai)料(du)后,基本给出的结论是这样的:如果字段是char或varchar类型,那么在字符串比较的时候MySQL使用PADSPACE校对规则,会忽略字段末尾的空格字符,这样然后我又查(搜)询(索)了一些资料,在stackoverflow上找到了一些较为专业的解答,先上地址:https://stackoverflow.com/questions/10495692/mysql-comparison-operator-spaces ,大意就是 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>. 嗯,看懂没。

  如果你有特殊需求,需要判断尾部的空格,对于字符串你可以使用  like ,如 like 'be   ',或者 使用 binary ,还有一种方法就是 使用 CHAR_LENGTH ,如  CHAR_LENGTH(name)=CHAR_LENGTH('be    ')。

  好了,就是这样,手动再见。

转载于:https://www.cnblogs.com/wudb/p/11213645.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值