检索,匹配度排序

本文提供了一个使用SQL进行复杂字符串匹配的示例,演示了如何根据特定条件筛选记录,并为每条记录计算一个匹配量来排序结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--生成测试数据
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值