Hi.
I have three tables:
player [id, name]
attribute [id, name]
player_attribute [id, player_id, attribute_id, value]
each player can have different attributes, some of them don't have any. Now I need to search for players with certain attributes, e.g. all players that have number 11, and their first name is John. At this moment I also know id's of these attributes, where
part of my query can look like: WHERE (attribute.id, attribute.value) in ((5, '11'), (18, 'John'))
I have to get all players that meets all requested attributes.
The problem is I don't know how the whole query have to look like? I tried joins, nested selects, grouping by but I'm not able to make it work. I'm always getting too much, or not enough rows.
Can you help me please?
EDIT Thanks to aranid I've found a solution:
select player_id from (
select * from player_attribute where (attribute.id, attribute.value) in ((5, '11'), (18, 'John')))
group by player_id
having count(*) = 2
so, the keyword was having by
used properly :)
Is there any way to transform above query into JOIN
statement?