MySql数值型字符串比较大小的坑

1.我们在做字符串数值比较大小的时候,通常会认为10比9大,但事实10却是最小的,怎么去解决这个问题,给出这样一个方案

2.现象

SELECT
	id,
	type_name AS typeName,
	type_code AS typeCode,
	pcode,
	tag_pic AS tagPic,
	is_del AS isDel 
FROM
	o2o_merchant_type 
ORDER BY
	type_code DESC;
select MAX(t1.type_code) as type_code from o2o_merchant_type as t1;
	

 我们发现字符串的10要比其他数值要小

我们发现求最大值却也是9而不是10

3.解决方法,在排序字段+0

SELECT
	id,
	type_name AS typeName,
	type_code AS typeCode,
	pcode,
	tag_pic AS tagPic,
	is_del AS isDel 
FROM
	o2o_merchant_type 
ORDER BY
	type_code + 0 DESC 
select MAX(t1.type_code + 0) as type_code from o2o_merchant_type as t1;

问题解决

一条路,名为黄泉,一座桥,名为奈何;一条河,名为忘川;三生石畔,圈尽缘。

愿,腰间配剑只为伊人

诚,三千发丝皆系于君

他,笑饮孟婆汤

她,不掬忘川泪

猜你喜欢

转载自blog.csdn.net/LuckFairyLuckBaby/article/details/93207328