电脑疯子技术论坛|电脑极客社区

 找回密码
 注册

QQ登录

只需一步,快速开始

[编程和数据库] mysql通过find_in_set()函数实现where in()顺序排序

[复制链接]
zhaorong 发表于 2017-10-16 12:16:52 | 显示全部楼层 |阅读模式

本文章来为各位介绍一篇关于mysql 实现按 where in () 中的顺序排序,
用find_in_set() 函数的教程,希望此教程能够对各位有所帮助。

  1. select * from table where id in ('783',' 769',' 814',' 1577',' 1769')
  2. 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...
去空格后:

  1. select * from table where id in ('783',' 769',' 814',' 1577',' 1769')
  2. 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通过find_in_set()函数实现where in()顺序排序的全部介绍。


您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|VIP|电脑疯子技术论坛 ( Computer madman team )

GMT+8, 2025-2-2 22:04

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表