question

Janne252 avatar image
Janne252 asked

Matching rows with a list of values/pattern

Hey, I'm trying to match rows based on a list of values. The list may vary in size; from one value to five values, e.g. (1, 2, 3) (1, 2) (34) (21, 4, 15, 16, 8) The rows I want to match contain five columns that I want to match with these lists: row# ability_01 ability_02 ability_03 ability_04 ability_05 [1] 1 2 3 4 5 [2] 16 17 8 9 2 [3] 21 17 8 6 2 [4] 7 15 32 1 16 With a list (1, 5) I would get row 1. With a list (8, 17) I would get rows 2 and 3. With a list (1, 2, 3, 4, 5) I would get row 1. With a list (7, 15, 1, 16) I would get row 4. I'm not sure if I'm actually trying to do something impossible. I'd like to do this with PHP but it would be better to do with SQL as pagination is already based on SQL. If you have suggestions for improving my DB structure, I would be interested to hear that as well.
sqllist
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

@SQLShark avatar image @SQLShark commented ·
Are you using MySQL or SQL Server?
0 Likes 0 ·
Janne252 avatar image Janne252 commented ·
I'm using MySQL. Case solved by normalizing DB structure and by using ability_id IN (1, 2, 3) group by column having count(*) = 3
0 Likes 0 ·

1 Answer

·
Squirrel avatar image
Squirrel answered
declare @tbl table ( row int, ability_01 int, ability_02 int, ability_03 int, ability_04 int, ability_05 int ) declare @list varchar(10) insert into @tbl select 1, 1, 2, 3, 4, 5 union all select 2, 16, 17, 8, 9, 2 union all select 3, 21, 17, 8, 6, 2 union all select 4, 7, 15, 32, 1, 16 select @list = '8,17' ; with cte as ( select * from @tbl unpivot ( ability for col in ([ability_01], [ability_02], [ability_03], [ability_04], [ability_05]) ) p ), list as ( select val = numberval, cnt = count(*) over() from dbo.CSVTable(@list) ) select c.row from cte c inner join list l on c.ability = l.val group by c.row having count(*) = max(l.cnt) CSVTable string parsing is from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=25830&SearchTerms=CSVTable
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Squirrel avatar image Squirrel commented ·
if you can normalize your table, then you can save the UNPIVOT step
0 Likes 0 ·
Janne252 avatar image Janne252 commented ·
Hey, thanks for the quick answer! How am I supposed to use this? Should this code snippet run without errors in phpMyAdmin? Edit: What do you mean by "normalize your table"?
0 Likes 0 ·
Squirrel avatar image Squirrel Janne252 commented ·
i am not familiar with php, but it will run in SQL Server. To Normalized your table means to change your table schema such that it removes any duplicate columns. So your table will be like - ID - Ability
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.