您好,欢迎来到测品娱乐。
搜索
您的当前位置:首页mysql通过find_in_set()函数实现wherein()顺序排序

mysql通过find_in_set()函数实现wherein()顺序排序

来源:测品娱乐


本文章来为各位介绍一篇关于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 还可实现多条件排序 试试哦

总结

Copyright © 2019- cepb.cn 版权所有 湘ICP备2022005869号-7

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务