3

数据库条目

表测试

 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中完成吗?

4

2 回答 2

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
于 2012-09-24T08:41:04.667 回答
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
于 2012-09-24T08:35:13.297 回答