Jquery中文网 www.jquerycn.cn
Jquery中文网 >  数据库  >  mysql  >  正文 mysql 实现按 where in () 中的顺序排序,用find_in_set() 函数

mysql 实现按 where in () 中的顺序排序,用find_in_set() 函数

发布时间:2017-03-21   编辑:www.jquerycn.cn
jquery中文网为您提供mysql 实现按 where in () 中的顺序排序,用find,in,set() 函数等资源,欢迎您收藏本站,我们将为您提供最新的mysql 实现按 where in () 中的顺序排序,用find,in,set() 函数资源
本文章来为各位介绍一篇关于mysql 实现按 where in () 中的顺序排序,用find_in_set() 函数的教程,希望此教程能够对各位有所帮助。

select * from table where id in ('783',' 769',' 814',' 1577',' 1769') 
order by find_in_set( id, '783, 769, 814, 1577, 1769' ) 
 
查出来:
 
769
1577
814
1769
783
 
为什么不是 783 769 814 1577 1769 的顺序?
注意:经查找后原因出在find_in_set里面,如果find_in_set的第二个参数中有空格将导致顺序乱掉,因为mysql查询之前不会给你trim空格符。
 
so...
去空格后:
 
select * from table where id in ('783',' 769',' 814',' 1577',' 1769') 
order by find_in_set( id, '783,769,814,1577,1769' ) 
 
注意只是去掉了

'783,769,814,1577,1769' 中的空格
 
再查出来:
 
783
769
814
1577
1769
 
至此我们实现用where in find_in_set 的排序,find_in_set 还可实现多条件排序 试试哦

您可能感兴趣的文章:
mysql 实现按 where in () 中的顺序排序,用find_in_set() 函数
mysql中的in排序 mysql按in中顺序来排序
MySQL 存储过程传参数实现where id in(1,2,3,...)的例子
mysql联合索引与Where子句优化浅析
mysql递归查询实现方法
mysql字段中使用逗号分隔符
mysql递归查询替代函数实例
java数组降序和升序排序例子
mysql根据汉字首字母排序的方法
PHP二维数组按照指定的字段排序的函数

[关闭]