如何在mysql中检查带有前导零的varchar [英] How to check varchar with leading zeros in mysql

查看:67
本文介绍了如何在mysql中检查带有前导零的varchar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面查询中的条件返回 true 结果,但我想要 false 结果.请推荐我.

condition in below query returning true result but i want false result. Please suggest me.

select *
from test    
where 
'00160001' between '0013001' and '0023000' OR 
'00200000' between '0013001' and '0023000'  

推荐答案

如果没有前导零,您会得到相同的结果.
使用整数数据类型来比较整数值.
您还可以使用强制转换即时转换值:

You get the same result without the leading zeros.
Use integer data type to compare integer values.
You can also convert the values on the fly using cast:

http://dev.mysql.com/doc/refman/5.7/en/cast-functions.html

您的 Cast 示例:

select if( cast('00160001' as unsigned) between cast('0013001' as unsigned)  
and cast('0023000' as unsigned),1,0) as test_a,  
if( cast('00200000' as unsigned) between cast('0013001' as unsigned)   
and cast('0023000' as unsigned),1,0) as test_b;

这篇关于如何在mysql中检查带有前导零的varchar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆