现在,我有:
Upper(tb.name) LIKE Upper('%' + :SearchBox + '%')
有了这个,我可以输入一个关键字,如果它存在于tb.name中,它将返回结果。我希望能够在SearchBox中输入多个关键字,用空格分隔,并返回包含所有关键字的结果。
示例:我在SearchBox中键入“Ford”并返回
Name
2015 Ford SUV Blue
2016 Ford SUV Black
2017 Ford SUV Blue
我希望能够输入“福特蓝”(或任何其他数量的关键字)来返回
Name
2015 Ford SUV Blue
2017 Ford SUV Blue
任何关键字输入都将以空格分隔。所以,如果我输入“SUV Blue 2017”,它将是“SUV”和“蓝色”和“2017”。 非常感谢任何帮助!
答案 0 :(得分:0)
您需要拆分关键字字符串并查找存在所有关键字字符串的行。这样的事情(见代码中的评论)。
declare @keywords varchar(max)='ford blue'
--build a series of CTE
;with split as ( --split keyword string into table
select '%'+value+'%' v from string_split(@keywords,' ')
),
cnt as ( -- always single value
select count(*) cnt from split
),
prod as(
select p.* ,
COUNT(t.v) over(partition by p.name) cnt,
ROW_NUMBER() over(partition by p.name order by p.name) rn
from tb p
--where ProductName like all -- illegal and never work
inner join split t on p.ProductName like t.v
)
select * from prod
cross join cnt -- don't afraid of Cartesian product here
where prod.cnt = cnt.cnt -- all keywords found
and rn=1 -- no duplicates
答案 1 :(得分:0)
将%放入空白字符串,例如:
WITH TB AS(
SELECT 'Ford SUV Blue' NAMES FROM DUAL UNION ALL
SELECT 'Ford SUV GREEN' NAMES FROM DUAL UNION ALL
SELECT 'SOME thing there' FROM DUAL
)
SELECT * FROM TB H
WHERE Upper(H.NAMES) LIKE Upper('%SOME% %thing%');