现在要实现这样一个功能,如果数据库中存有数据如
a
ab
abc
abcdd
abcde
abcdf
但是我现在输入的查询条件是abcda,这样能查出与abcda最相近的单词
abcdd
abcde
abcdf
a
ab
abc
abcdd
abcde
abcdf
但是我现在输入的查询条件是abcda,这样能查出与abcda最相近的单词
abcdd
abcde
abcdf
关注:select * from tb where name LIKE 'abcda[a-z]' or charindex(substring('abcda',1,len('abcda')-1),'abcda')>1
insert into #
select 'a'
union all select 'ab'
union all select 'abc'
union all select 'abcdd'
union all select 'abcde'
union all select 'abcdf'declare @a varchar(10)
set @a='abcda'
select * from # where code like left(@a,len(@a)-1)+'%'drop table #
WHERE Len(code) = Len('abcda')
ORDER BY DIFFERENCE('abcda', code) desc
2.看返回结果集数量满不满足要求,如果需要其它相似结果,进行下一步。
3.将abcda进行截位后模糊查询处理
首先先截一位,得到abcd,用模糊查询 C1 LIKE 'abcd*'
检查返回结果集数量,如需继续查询更多相似结果,则再截一位后用 C1 LIKE ‘abc*' 来查询。这种方式非常灵活,完全由自己控制。
{
查询字串=截串函数(查询字串,1 , 字串长度函数(查询字串)- I );
//第一次执行时,I=0,得到结果仍是原串
if (i==0)
{sqlStr = "SELECT * FROM 表 where c1='" + 查询字串 + "'";}
else
{sqlStr = "SELECT * FROM 表 where c1 LIKE '" + 查询字串 + "*'";}
执行查询
判断查询结果,如果满足要求,不打算继续查询,则退出循环
}
insert into #
select 'a'
union all select 'ab'
union all select 'abc'
union all select 'abcdd'
union all select 'abcde'
union all select 'abcdf'declare @a varchar(10)
set @a='abcda'
select * from # where code like left(@a,len(@a)-1)+'%'drop table #code
----------
abcdd
abcde
abcdf(所影响的行数为 3 行)沙果老弟的方法不错.
select * from table_name
where code like 'abcd_'各位大师我是新手,不对的话请原谅!!
order by 要查询的字段