数据库条目
表格测试
name
========================
Sunae Kasimov
Obsop Natty
Preem Kuman
Mysql查询
select *
from test
where name like 'na%' or
name like '% na%' or
name like '%na%';
按顺序返回 1然后2 但我想按顺序2和1.那就是要给'%na%'然后'%na%'更多的权重
这有什么办法可以在mysql中完成吗?
答案 0 :(得分:2)
select * from
(
select * from name where name like '% na%'
union
select * from name where name like 'na%'
union
select * from name where name like '%na%'
) a
答案 1 :(得分:2)
您应该在您的选择和订单中创建虚拟字段。例如:
select test.*,
case when name like '% na%'
then 1
else 2
end as OrderField
from test
where name like 'na%' or
name like '% na%' or
name like '%na%'
order by OrderField