日期:2014-05-17  浏览次数:20492 次

模糊查询,分词。在线等高手解答
topic表如下:
id title 
1 abc英语字母
2 中国崛起
3 装b的境界


模糊查询我想要的是根据title 模糊查询,连续的英文字母作为一个整体。 如模糊查询的内容为“abc英语”,其查询的结果与
“select * from topic where title lLIKE '%abc%' AND'%英%' AND'%语%'”。即查询结果为:
id title 
1 abc英语字母

------解决方案--------------------
太智能化了,估计要用到类似百度的搜索功能.帮顶.
------解决方案--------------------
找google吧
------解决方案--------------------
SQL code

select * from topic
where charindex('abc',title)>0
and charindex('英',title)>0
and charindex('语',title)>0

------解决方案--------------------
关键是把查询内容拆分成几个字符串,然后拼SQL:
SQL code

declare @s varchar(50), @sql varchar(8000)
set @sql = 'select * from topic where title like ''%'
set @s = 'abc英语'
declare @i int
set @i = 1
while @i <= len(@s)
begin
    if @i = 1
        set @sql = @sql + substring(@s,@i,1)
    else
    begin
        if len(substring(@s,@i,1)) <> datalength(substring(@s,@i-1,1)) or datalength(substring(@s,@i,1)) = 2
            set @sql = @sql + '%'' and title like ''%' + substring(@s,@i,1)
        else
            set @sql = @sql + substring(@s,@i,1)
    end

    set @i = @i + 1
end
set @sql = @sql + '%'''

exec(@sql)