数据库模糊查询,类似百度。如关键字是“瑞士大酒店”,可搜出结果“瑞士帝国大酒店”,“瑞士宾馆”,“瑞瑞大酒店”,“上海瑞士大酒店”等。
解决方案 »
- sql如何将同时满足两个条件的重复值的记录去掉
- provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接,,,求细节,求解答!
- 昨晚改了用sql内置的全文检索服务现在IIs进程17%?
- WinXP下安装MSSql7第一次成功,卸载后再安装就不行了,郁闷!各位帮帮忙了!
- sql server 2005 问题! 急~~~~~~~~~~~~~
- 配置分发服务器出错 8512 将截断字符串或二进制数据 解决了马上给分
- 数据库强制还原的问题
- 关于存储过程的返回数据集问题
- 从Sql Server 2000导出到Access的问题!
- 这样的存储过程效率高吗?
- 这个sql怎么写?
- 最简短替换语句
--创建函数create function [dbo].[m_fuzzyquery_v1]( @str nvarchar(2000)) returns nvarchar(2000)as begin declare @count int,@i int; declare @newchar nvarchar(200),@nn nvarchar(300),@hh nvarchar(200) set @count=len(@str);set @i=1;set @nn=''; while @i<@count+1 begin set @newchar=substring(@str,@i,1)+'%' set @nn=@nn+@newchar; set @i=@i+1; end set @hh='%'+@nn return @hh end --测试数据declare @table table (connect varchar(30))insert into @tableselect '我爱程序' union allselect '我的程序生活' union allselect '绝对无聊的生活' union allselect '活得好累' union allselect '程序员的生活' union allselect '序论' union allselect '生机' union allselect '生活杂志' union allselect '我只是随便写写' union allselect '真的是要来搜索的' union allselect '程序员一生的活路' --普通的模糊查询select * from @table where connect like '%程序生活%'--运行结果/*connect------------------------------我的程序生活*/ --应用函数查询select * from @table where connect like ( select dbo.[m_fuzzyquery_v1]('程序生活'))--运行结果/*connect------------------------------我的程序生活程序员的生活程序员一生的活路*/ 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/maco_wang/archive/2011/03/16/6254963.aspx
比如 你5个汉字 首先 5个全匹配的 然后4个 然后3个
匹配会写吧
用like '%%'
可以把条件拆成符合1个的,符合2个的,符合3个的,4个的,和完全匹配的,情况比较多,穷举后or可以满足你的要求,但是你知道这需要多少个or吗?貌似不现实!
if OBJECT_ID('p_search','p') is not null
drop procedure p_search
go
create procedure p_search
@keyword varchar(50)
as
declare @sqlstr varchar(max);--sqlstring
declare @i int;--循环记数
declare @length int;--keyword长度
declare @wherestr varchar(max);--wherestring
set @length=LEN(@keyword);set @i=1;set @sqlstr='select * from 表名 where ';set @wherestr='';
while @i<=@length
begin
if(@wherestr!='')
begin
set @wherestr+='or 列名 like ''%'+SUBSTRING(@keyword,@i,1)+'%'' ';
set @i=@i+1
end
else
set @wherestr+='列名 like ''%'+SUBSTRING(@keyword,@i,1)+'%'' ';
set @i=@i+1
end
set @sqlstr=@sqlstr+@wherestr
select @sqlstr as sqlstr--输出sql语句,检查语句是否正确
exec(@sqlstr)
go
maco_wang说的匹配度,应该指“酒店”、“瑞士”这样的词吧?不知道你有没有更好的方法?