I am trying to find the most efficient way to get the items of an Array that were split and matching it to a column.
for example Array [mouse, cat, dog]
i need to find a row where column animal has all 3 items (mouse, cat, dog) i found ways to get if any of those items where found but not all. i can do it using recursive but its not ideal because i am also going to have to search other columns as well for the same field.
declare @Search varchar(100)
set @Search = 'mouse dog cat';
DECLARE @tblWords TABLE( ID int,ArrayValue VARCHAR(500))
INSERT INTO @tblWords (ID, ArrayValue) SELECT * FROM [dbo].[Splitstring] (@Search, ' ')
SELECT ID, animal ,COUNT(*) as [Result]
FROM @tblWords
JOIN zoo
on animal like '%' + ArrayValue + '%'
GROUP BY ID, animal
this result will find if any of those 3 items found but not all of them. what am i doing wrong.
You need a having
clause to count the number of matches:
SELECT z.ID, z.animal, COUNT(*) as [Result]
FROM @tblWords w JOIN
zoo z
on z.animal like '%' + w.ArrayValue + '%'
GROUP BY z.ID, z.animal
HAVING count(*) = (select count(*) from @tblWords);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.