请问如何实现查询字符串中有空格的模糊查询问题

niedx2007 2008-11-14 09:32:26
请教各位,如何像百度那样从书的数据库表中实现模糊查询:比如输入:" C 精髓 教程 "从数据库中就能把含有上面关键字的书全部找出来,注意字符串中间是有空格的,个数不定,谢谢
...全文
754 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
geodetic 2008-11-14
  • 打赏
  • 举报
回复
数据量大全文检索
数据量小的话
--字符串分割存入临时表函数
CREATE FUNCTION [dbo].[SplitStr]
(
-- Add the parameters for the function here
@Str VARCHAR(8000), --将要处理的字符串
@SepStr VARCHAR(8000) --分隔符
)
RETURNS
@Tbl TABLE
(Item VARCHAR(8000)
)
AS
BEGIN
DECLARE @ItemString VARCHAR(8000)
DECLARE @itemIndex INT

SET @Str = @Str + @SepStr

WHILE(CHARINDEX(@SepStr, @Str) > 0 AND @Str <> @SepStr)
BEGIN
SET @itemIndex = CHARINDEX(@SepStr,@Str)
SET @ItemString = SUBSTRING(@Str, 1, @itemIndex - 1)

INSERT INTO @Tbl
SELECT @ItemString

SET @Str = SUBSTRING(@Str, @itemIndex + LEN(@SepStr), LEN(@Str) - @itemIndex)
END

RETURN
END
GO

declare @tblName varchar(50),@sql varchar(100)
declare cur CURSOR FORWARD_ONLY for
select name from SplitStr('C 精髓 教程',' ')

open cur
fetch next from cur into @item
while @@fetch_status=0
begin
select * from table1 where xx like '%'+@item+'%'
fetch next from cur into @item
end
close cur
deallocate cur
GO
powerST 2008-11-14
  • 打赏
  • 举报
回复
全文检索实现模糊查询很快,而且在全文检索中:" C 精髓 教程",这样可以搜索出不同顺序组合的文本
参透对数据库数据进行文本搜索
里面有非常详细的介绍
http://myspace.62368.com/Articles.aspx?id=1065
viva369 2008-11-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wufeng4552 的回复:]
全文搜索
[/Quote]
~~~
niedx2007 2008-11-14
  • 打赏
  • 举报
回复
declare @inputstr=" C 精髓 教程 "

declare @i int
set @inputstr = rtrim(ltrim(@inputstr))
set @i = charindex('', @inputstr)
if @i >= 2
begin
replace(@inputstr ,@i,'%')
end
go

select * from 书 where booknam like '%+(@inputstr +%';

这样写可以吗
niedx2007 2008-11-14
  • 打赏
  • 举报
回复
怎么把空格用%代替呢
水族杰纶 2008-11-14
  • 打赏
  • 举报
回复
全文搜索
百年树人 2008-11-14
  • 打赏
  • 举报
回复
空格用%代替?

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧