mysql字符串排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013028876/article/details/78949161

        mysql中对字符串排序,字符串中有数字有汉字,想按数字的大小来进行排序。仅仅用order by排序,效果不是想要的。

        sql语句为:

select id,dict_name,type_code from t_dictionary  where type_code='GRADE' ORDER BY `dict_name`;

        排序效果如下:


        因为字符串排序是先比较字符串第一个字符的大小。而不是像int型比较整个数字的大小。要想用字符串数据排成整型排序的效果,可以采用如下三种方式:

select id,dict_name,type_code from t_dictionary  where type_code='GRADE' ORDER BY `dict_name`*1;
select id,dict_name,type_code from t_dictionary  where type_code='GRADE' ORDER BY dict_name+0;
select id,dict_name,type_code from t_dictionary  where type_code='GRADE' ORDER BY CAST(dict_name AS DECIMAL);

        效果图均为:


总结:

       记录下来,收获感比较强,积累成就感。

       若还有其他的实现方式,可以留言交流~~


参考文章:

       https://www.cnblogs.com/yako/archive/2012/03/01/2375584.html 

       http://database.51cto.com/art/201011/235318.htm


猜你喜欢

转载自blog.csdn.net/u013028876/article/details/78949161