如何在mysql中使用前导零检查varchar

时间:2016-05-30 14:41:44

标签: mysql

下面的查询条件返回true结果,但我想要错误的结果。请建议我。

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

1 个答案:

答案 0 :(得分:0)

如果没有前导零,则会得到相同的结果 使用整数数据类型来比较整数值 您还可以使用cast:

动态转换值
  

{{3}}

您使用演员的示例:

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;