mysql通過find_in_set()函式實現where in()順序排序
阿新 • • 發佈:2020-01-09
本文章來為各位介紹一篇關於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',1769' )
注意只是去掉了
'783,1769' 中的空格
再查出來: 783 769 814 1577 1769
至此我們實現用where in find_in_set 的排序,find_in_set 還可實現多條件排序 試試哦
總結
以上就是本文關於mysql通過find_in_set()函式實現where in()順序排序的全部介紹,感興趣的朋友可以參閱:MySQL資料庫表分割槽注意事項大全【推薦】、幾個比較重要的MySQL變數、sql和MySQL的語句執行順序分析等,若有不足之處,歡迎大家留言指正。希望對大家有所幫助。