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