mssql查询自动complate

时间:2017-12-02 20:03:20

标签: sql-server tsql autocomplete

当我质疑一个单词时,它会完成单词或为其添加新单词。 独特的前10个录音。

declare @Category table (CategoryName  varchar(50)) 

insert into @Category (CategoryName) values('phones')
insert into @Category (CategoryName) values('phone blue')
insert into @Category (CategoryName) values('phone pink')
insert into @Category (CategoryName) values('phone big')
insert into @Category (CategoryName) values('phone big bang')
insert into @Category (CategoryName) values('bad phone blue')
insert into @Category (CategoryName) values('bad phone pink')
insert into @Category (CategoryName) values('bad phone but good')

在调用类别名称“phone”

时,我希望得到以下结果
  • 手机蓝色
  • 手机粉红色
  • 手机大
  • 电话,但

在调用类别名称“phon”

时,我希望得到以下结果
  • 电话
  • 电话

2 个答案:

答案 0 :(得分:0)

declare @Category table (CategoryName  varchar(50)) 

insert into @Category (CategoryName) values('phone blue')
insert into @Category (CategoryName) values('phone pink')
insert into @Category (CategoryName) values('phone big')
insert into @Category (CategoryName) values('phone big bang')
insert into @Category (CategoryName) values('bad phone blue')
insert into @Category (CategoryName) values('bad phone pink')
insert into @Category (CategoryName) values('bad phone but good')
insert into @Category (CategoryName) values('phone')
insert into @Category (CategoryName) values('silly phone')
insert into @Category (CategoryName) values('lost phone ')
insert into @Category (CategoryName) values('white phone user')
insert into @Category (CategoryName) values('some device')
insert into @Category (CategoryName) values('phoner homer')
insert into @Category (CategoryName) values('space phone  ')
insert into @Category (CategoryName) values('jam phone  hex doc')

declare @Formword varchar(50) = 'phone';

select    CategoryName
        --, charindex(@FormWord,Categoryname)
        --, charindex(@FormWord,Categoryname) + len(@FormWord)-1
        --, charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord))
        --, ltrim(right(categoryname,(len(categoryname)-charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord)))))
        --, charindex(' ',ltrim(right(categoryname,(len(categoryname)-charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord))))))
        --, left(ltrim(right(categoryname,(len(categoryname)-charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord))))),charindex(' ',ltrim(right(categoryname,(len(categoryname)-charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord)))))))
        , case  when charindex(' ',ltrim(right(categoryname,(len(categoryname)-charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord)))))) = 0
                    then @Formword + ' ' + ltrim(right(categoryname,(len(categoryname)-charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord)))))
                else 
                    @Formword + ' ' + left(ltrim(right(categoryname,(len(categoryname)-charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord))))),charindex(' ',ltrim(right(categoryname,(len(categoryname)-charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord)))))))
          end [next word]

from @Category
where 1 = 1
        and CategoryName like '%' + @Formword + '%'
        and len(CategoryName) > len(@Formword) + 1
        and charindex(@FormWord,Categoryname) + len(@FormWord) < len(Categoryname)

答案 1 :(得分:0)

declare @Category table (CategoryName  varchar(50)) 

insert into @Category (CategoryName) values('phones')
insert into @Category (CategoryName) values('phone blue')
insert into @Category (CategoryName) values('phone pink')
insert into @Category (CategoryName) values('phone big')
insert into @Category (CategoryName) values('phone big bang')
insert into @Category (CategoryName) values('bad phone blue')
insert into @Category (CategoryName) values('bad phone pink')
insert into @Category (CategoryName) values('bad phone but good')


declare @searchWord varchar(50) = 'phone';
declare @res int;

set @res = (select case when (select count(*) from @Category
                 where substring(CategoryName, 0, CHARINDEX(' ', CategoryName)) = @searchWord) > 0
                 then 1
                 else 0
            end)
if(@res = 1)
begin
    select top 10 CategoryName from @Category where CategoryName like''+@searchWord+'%' and CategoryName like '% %' order by CategoryName
end
else
begin
    select top 10 CategoryName from @Category where CategoryName like''+@searchWord+'%' and CategoryName not like '% %' order by CategoryName
end

我希望这会有所帮助。