最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
mysql 实现按 where in () 中的顺序排序,用find_in_set() 函数
时间:2016-05-10 编辑:简简单单 来源:一聚教程网
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登录、访问及退出操作解析 10-18
- sql语句 update字段null不能用is null问题解析 09-28
- SQL Server ISNULL 不生效原因及解决分析 09-28
- 关于if exists的用法及说明分析 09-28
- Access数据库日常维护方法 09-28
- Oracle 删除大量表记录操作介绍 09-27