600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > mysql自定义排序以及优化like模糊查询

mysql自定义排序以及优化like模糊查询

时间:2022-05-25 13:13:00

相关推荐

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 1when city like '%郑州%' then 2when city like '%开封%' then 3when city like '%平顶山%' then 4when city like '%洛阳%' then 5when city like '%商丘%' then 6when city like '%安阳%' then 7when city like '%新乡%' then 8when city like '%鹤壁%' then 9when city like '%焦作%' then 10when city like '%濮阳%' then 11when city like '%漯河%' then 12when city like '%三门峡%' then 13when city like '%周口%' then 14when city like '%驻马店%' then 15when city like '%南阳%' then 16when city like '%信阳%' then 17when city like '%济源%' then 18endasc

网上有一些关于like %xxx%的优化建议,比如 locate, instr, position的方式,但是亲测之后发现这几个方式也是全表扫描。如果数据量庞大的话,还是建议直接使用搜索引擎elasticsearch。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。