使用搜索字符串的mysql顺序
作者:互联网
我有一个mysql查询,例如:
select * from employee
where name like '%ani%'
我希望我的结果可以从ani开始进行排序
例如我的结果应该是:
{anil,anirudha,rani,…}首先以ani开头,然后是其他字符.
有人可以帮我吗?
解决方法:
编辑:添加了第二个解决方案.
解决方案1:
select 1 AS OrderField, name
from employee
where name like 'ani%'
UNION ALL
select 2 AS OrderField, name
from employee
where name like '_%ani%'
ORDER BY OrderField, name
解决方案2:
select name, CASE WHEN name LIKE 'ani%' THEN 1 ELSE 2 END OrderField
from employee
where name like '%ani%'
order by OrderField, name
标签:sql-order-by,sql-like,string,mysql 来源: https://codeday.me/bug/20191207/2087422.html