I have a MySQL table like this:
+----+------------+----------+
| id | instanceID | answerID |
+----+------------+----------+
| 1 | 1 | 10 |
| 2 | 1 | 12 |
| 3 | 1 | 20 |
| 4 | 2 | 12 |
| 5 | 2 | 20 |
| 6 | 2 | 22 |
| 7 | 2 | 25 |
| 8 | 3 | 20 |
| 9 | 3 | 25 |
| 10 | 4 | 12 |
| 11 | 4 | 20 |
+----+------------+----------+
I would like to retrieve the instanceID under certain conditions.
This is easy using ‘OR’:
SELECT instanceID FROM table WHERE (answerID = 10 OR answerID = 12) GROUP BY instanceID;
Returns:
1,2,4
However, ‘AND’ is not so simple, as the condition is applied to each individual row, rather than each instanceID.
For example, I would like to retrieve the instanceID where (answerID = 20 AND answerID = 25)
which should return 2,3
I need to avoid subqueries like WHERE x IN (subquery), as the table is biIIIIig.
This is a difficult concept to search for in search engines, so I resort to asking it here.
Thanks!
How about something like this:
See SQL Fiddle with Demo
Or even:
See SQL Fiddle with Demo