T-SQL 在所有列中搜索 jquery 自动完成文本框
T-SQL search through all columns for jquery autocomplete textbox
我一直在尝试设计一个查询来搜索所有列中的特定文本,但它没有按要求工作。
这是我的查询:
create table #tblTempAddress (locationId int identity(1,1) primary key, postcode nvarchar(300), road nvarchar(1000), ApartmentName nvarchar(1000), district nvarchar(200), Village nvarchar(200), City nvarchar(200))
insert into #tblTempAddress values ('DY4 8QJ','Union Street',NULL,'Tipton',NULL,NULL)
insert into #tblTempAddress values ('DY4 9JP','Phillips Court','Union Street','Princes End',NULL,NULL)
insert into #tblTempAddress values ('DY4 9JR','Union Street','Princes End','Tipton',NULL,NULL)
insert into #tblTempAddress values ('DY8 1PJ','Union Street',NULL,'Stourbridge',NULL,NULL)
insert into #tblTempAddress values ('DY8 1PR','Union Street',NULL,'Stourbridge',NULL,NULL)
insert into #tblTempAddress values ('DY9 8BJ','Union Street','Lye','Stourbridge',NULL,NULL)
insert into #tblTempAddress values ('B65 0EL','Union Street',NULL,'Rowley Regis',NULL,NULL)
insert into #tblTempAddress values ('B65 0ER','Union Street',NULL,'Rowley Regis',NULL,NULL)
insert into #tblTempAddress values ('DY2 8PJ','Union Street',NULL,'Dudley',NULL,NULL)
insert into #tblTempAddress values ('DY2 8PP','Union Street',NULL,'Dudley',NULL,NULL)
declare @searchtext nvarchar(1000)
set @searchtext = 'union street'
select top 10 postcode, road, ApartmentName, District, Village, City from #tblTempAddress
where road like '%' + @searchtext + '%'
OR ApartmentName like '%'+ @searchtext + '%'
OR District like '%'+ @searchtext + '%'
OR Village like '%'+ @searchtext + '%'
OR City like '%'+ @searchtext + '%'
OR postcode like '%' + @searchtext + '%'
当我将@searchtext 设置为 "union street" 时,它 returns 所有与 "union street" 相关的数据,但是当我添加 "lye" 时,如 @searchtext = 'union street lye'
然后它不是显示与第 6 行上的 union street Lye 相关的结果。
我浏览了 this 博客,但没有任何帮助。
你可以这样做。
--your table declaration here
declare @searchtext nvarchar(1000)
set @searchtext = 'union street in lye'
;with keywords as (
select value from string_split(@searchtext,' ') --tokenize @searchtext
except
select * from string_split('and or a an the for at on in why how when to from',' ')--exclude stop words
),
kwcount as (
select COUNT(value) total from keywords
),
matches as (
select t.locationId
,COUNT(distinct value) cnt
from #tblTempAddress t
inner join keywords on concat(postcode,' ', road,' ', ApartmentName,' ', district,' ', Village,' ', City) like '%'+keywords.value+'%'
group by t.locationId
)
select top 10 postcode, road, ApartmentName, District, Village, City
from #tblTempAddress t
inner join matches m on t.locationId=m.locationId
cross join kwcount c
where m.cnt=c.total --require match all keywords in @searchtext. weaker filter may be used
order by m.cnt desc
查询可能会给出一些误报,因此需要调整过滤器。
全文搜索也很有帮助。
我一直在尝试设计一个查询来搜索所有列中的特定文本,但它没有按要求工作。
这是我的查询:
create table #tblTempAddress (locationId int identity(1,1) primary key, postcode nvarchar(300), road nvarchar(1000), ApartmentName nvarchar(1000), district nvarchar(200), Village nvarchar(200), City nvarchar(200))
insert into #tblTempAddress values ('DY4 8QJ','Union Street',NULL,'Tipton',NULL,NULL)
insert into #tblTempAddress values ('DY4 9JP','Phillips Court','Union Street','Princes End',NULL,NULL)
insert into #tblTempAddress values ('DY4 9JR','Union Street','Princes End','Tipton',NULL,NULL)
insert into #tblTempAddress values ('DY8 1PJ','Union Street',NULL,'Stourbridge',NULL,NULL)
insert into #tblTempAddress values ('DY8 1PR','Union Street',NULL,'Stourbridge',NULL,NULL)
insert into #tblTempAddress values ('DY9 8BJ','Union Street','Lye','Stourbridge',NULL,NULL)
insert into #tblTempAddress values ('B65 0EL','Union Street',NULL,'Rowley Regis',NULL,NULL)
insert into #tblTempAddress values ('B65 0ER','Union Street',NULL,'Rowley Regis',NULL,NULL)
insert into #tblTempAddress values ('DY2 8PJ','Union Street',NULL,'Dudley',NULL,NULL)
insert into #tblTempAddress values ('DY2 8PP','Union Street',NULL,'Dudley',NULL,NULL)
declare @searchtext nvarchar(1000)
set @searchtext = 'union street'
select top 10 postcode, road, ApartmentName, District, Village, City from #tblTempAddress
where road like '%' + @searchtext + '%'
OR ApartmentName like '%'+ @searchtext + '%'
OR District like '%'+ @searchtext + '%'
OR Village like '%'+ @searchtext + '%'
OR City like '%'+ @searchtext + '%'
OR postcode like '%' + @searchtext + '%'
当我将@searchtext 设置为 "union street" 时,它 returns 所有与 "union street" 相关的数据,但是当我添加 "lye" 时,如 @searchtext = 'union street lye'
然后它不是显示与第 6 行上的 union street Lye 相关的结果。
我浏览了 this 博客,但没有任何帮助。
你可以这样做。
--your table declaration here
declare @searchtext nvarchar(1000)
set @searchtext = 'union street in lye'
;with keywords as (
select value from string_split(@searchtext,' ') --tokenize @searchtext
except
select * from string_split('and or a an the for at on in why how when to from',' ')--exclude stop words
),
kwcount as (
select COUNT(value) total from keywords
),
matches as (
select t.locationId
,COUNT(distinct value) cnt
from #tblTempAddress t
inner join keywords on concat(postcode,' ', road,' ', ApartmentName,' ', district,' ', Village,' ', City) like '%'+keywords.value+'%'
group by t.locationId
)
select top 10 postcode, road, ApartmentName, District, Village, City
from #tblTempAddress t
inner join matches m on t.locationId=m.locationId
cross join kwcount c
where m.cnt=c.total --require match all keywords in @searchtext. weaker filter may be used
order by m.cnt desc
查询可能会给出一些误报,因此需要调整过滤器。
全文搜索也很有帮助。