匹配量排序

来源:互联网 发布:js array 清空数组 编辑:程序博客网 时间:2024/05/17 20:25

大笑微笑生气吐舌头吐舌头吐舌头吐舌头吐舌头吐舌头吐舌头吐舌头吐舌头吐舌头吐舌头生气微笑大笑


偷笑我爱北京天安门偷笑


偷笑天安门上太阳升偷笑


偷笑伟大领袖毛主席偷笑


偷笑指引我们向前进偷笑

偷笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑偷笑

--生成测试数据
declare @t table(id int,name varchar(20))
insert into @t select 1,'中国湖南长沙芙蓉区'
insert into @t select 2,'中国福建福州鼓楼区'
insert into @t select 3,'中国北京北京朝阳区'
insert into @t select 4,'中国河南郑州金水区'

--查询示例
select
    *,
   (case when name like '%中国%' then 1 else 0 end +
    case when name like '%湖南%' then 1 else 0 end +
    case when name like '%长沙%' then 1 else 0 end +
    case when name like '%州%'   then 1 else 0 end) as 匹配量
from
    @t
where
    name like '%中国%' or name like '%湖南%' or name like '%长沙%' or name like '%州%'
order by
    匹配量 desc

--输出结果
/*
id          name                 匹配量        
----------- -------------------- -----------
1           中国湖南长沙芙蓉区    3
2           中国福建福州鼓楼区    2
4           中国河南郑州金水区    2
3           中国北京北京朝阳区    1
*/

原创粉丝点击