日期:2014-05-17  浏览次数:20367 次

求查询结果排序问题,谢谢

select  * from 表名  where 字段  like '%a%' or 字段  like '%b%' or 字段  like '%c%' 

A条件            B条件         C条件


只要满足条件之一就要显示出来。

另外显示顺序: 搜索结果排列
满足ABC
满足AB   AC
满足A
满足BC
满足B
满足C

------解决方案--------------------
select 字段,xxx
from (
select  字段,'满足'+case when CHARINDEX('a',字段)>0 then 'A' else '' end+
case when CHARINDEX('b',字段)>0 then 'B' else '' end+
case when CHARINDEX('c',字段)>0 then 'C' else '' end as xxx
from 表名  
where 字段  like '%a%' or 字段  like '%b%' or 字段  like '%c%' 
)t
order by xxx

------解决方案--------------------
try this,

select *
 from 表名
 where 字段 like '%a%' or 字段 like '%b%' or 字段 like '%c%'
 order by  
 case when 字段 like '%a%' and 字段 like '%b%' and 字段 like '%c%' then 1
      when (字段 like '%a%' and 字段 like '%b%') or (字段 like '%a%' and 字段 like '%c%') then 2
      when 字段 like '%a%' then 3
      when 字段 like '%b%' and 字段 like '%c%' then 4
      when 字段 like '%b%' then 5
      when 字段 like '%c%' then 6 end