mysql自定义排序以及优化like模糊查询
生活随笔
收集整理的這篇文章主要介紹了
mysql自定义排序以及优化like模糊查询
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
**1. 自定義排序函數FIELD()**SELECT id,username,city FROM sy_user order byFIELD(city,'鄭州', '開封', '平頂山','洛陽', '商丘', '安陽', '新鄉', '許昌', '鶴壁', '焦作', '濮陽', '漯河', '三門峽', '周口','駐馬店', '南陽', '信陽', '濟源','省本部','河南')
**2.使用 case when**SELECT id,username,city FROM `sy_user` ORDER BY case
when city like '%省%' then 1
when city like '%鄭州%' then 2
when city like '%開封%' then 3
when city like '%平頂山%' then 4
when city like '%洛陽%' then 5
when city like '%商丘%' then 6
when city like '%安陽%' then 7
when city like '%新鄉%' then 8
when city like '%鶴壁%' then 9
when city like '%焦作%' then 10
when city like '%濮陽%' then 11
when city like '%漯河%' then 12
when city like '%三門峽%' then 13
when city like '%周口%' then 14
when city like '%駐馬店%' then 15
when city like '%南陽%' then 16
when city like '%信陽%' then 17
when city like '%濟源%' then 18
end
asc
網上有一些關于like %xxx%的優化建議,比如 locate, instr, position的方式,但是親測之后發現這幾個方式也是全表掃描。如果數據量龐大的話,還是建議直接使用搜索引擎elasticsearch。
總結
以上是生活随笔為你收集整理的mysql自定义排序以及优化like模糊查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 查看计算机或网络资源列表的命令,dos命
- 下一篇: mysql修改表结构例子_mysql修改