I have encountered a sort of problem which I couldn't find true words to explain it, imagine I have some foreign keys that have some key-value pairs:
10 => (2, 3) , (4, 5)
11 => (2, 1), (4, 5)
It can be mapped in the following table:
ID | FK | Key | Value
-----------------------------
1 | 10 | 2 | 3
2 | 10 | 4 | 5
3 | 11 | 2 | 1
4 | 11 | 4 | 5
I want a query to find all FKs that their pairs are (2, 3) AND (4, 5)
which the answer is FK => 10
and that's where the problem appears!
If I use AND
SELECT FK
FROM MyTable
WHERE (Key = 2 AND Value = 3)
AND
(Key = 4 AND Value = 5)
It returns nothing and if I use OR:
SELECT FK
FROM MyTable
WHERE (Key = 2 AND Value = 3)
OR
(Key = 4 AND Value = 5)
it returns 10 and 11 as answer which 11 is not the answer.
Actually I need more complicated query in my situation since I need to find a case that matches more than two key-value pairs.