Home  >  Article  >  Database  >  MySQL关于字符串中数字排序的问题分析_MySQL

MySQL关于字符串中数字排序的问题分析_MySQL

WBOY
WBOYOriginal
2016-07-06 13:32:451138browse

本文实例讲述了MySQL关于字符串中数字排序的问题。分享给大家供大家参考,具体如下:

MySQL字符串相信大家都不陌生,在MySQL字符串排序时经常会遇到一些问题,比如下面要介绍的这个

今天解决了一个关于MySQL字符串排序的很奇怪的问题,在数据里面定义的是varchar类型,实际存放的是Int类型的数据,按一下查询语句进行排序:
将字段*1或者+0可以将MySQL字符串字段按数值排序

如:

select * from table where 1 order by id*1 desc;

或者

select * from table where 1 order by id+0 desc;

除了上述方法外,这里附上一种排序方法,利用find_in_set()进行无敌排序

附上Mysql函数 find_in_set() 的用法:

代码如下:

$sql = "select p.*, find_in_set(p.products_id,$string_hot_pid) as rank from products p where p.products_id in ($string_hot_pid) order by rank";

大家有什么好的想法和建议可以留下宝贵的意见 以便共同进步

更多关于MySQL相关内容感兴趣的读者可查看本站专题:《MySQL日志操作技巧大全》、《MySQL事务操作技巧汇总》、《MySQL存储过程技巧大全》、《MySQL数据库锁相关技巧汇总》及《MySQL常用函数大汇总》

希望本文所述对大家MySQL数据库计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn