数据库模糊查询,类似百度。如关键字是“瑞士大酒店”,可搜出结果“瑞士帝国大酒店”,“瑞士宾馆”,“瑞瑞大酒店”,“上海瑞士大酒店”等。

解决方案 »

  1.   


    --创建函数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
      

  2.   

    这个是 多次匹配的  
    比如 你5个汉字  首先 5个全匹配的 然后4个 然后3个
    匹配会写吧
    用like '%%'
      

  3.   

    谢谢你的回复,你的方法属于把字段拆开,再like.但是你没加or或and,如果加上or,是否能解决我的问题。我的拙见
      

  4.   

    关键词是5个汉字,我做的是完全匹配,但是你要的结果是部分匹配。
    可以把条件拆成符合1个的,符合2个的,符合3个的,4个的,和完全匹配的,情况比较多,穷举后or可以满足你的要求,但是你知道这需要多少个or吗?貌似不现实!
      

  5.   


    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
      

  6.   

    根据maco_wang提示写的存储过程。请多指教。
    maco_wang说的匹配度,应该指“酒店”、“瑞士”这样的词吧?不知道你有没有更好的方法?