I’m having trouble forming my query for a user assessment table I have.
Currently the table looks like this.
+-----+----------+----------+
| id | video_id | user_id |
+-----+----------+----------+
| 1 | 1 | 1 |
| 2 | 2 | 50 |
| 3 | 1 | 115 |
| 4 | 2 | 25 |
| 5 | 2 | 1 |
| 6 | 6 | 98 |
| 7 | 3 | 1 |
+-----+----------+----------+
The issue I’m running into is forming a query that returns the user_id for someone who has watched video_id’s of 1, 2, and 3. So in the above example, only user_id 1 should be returned.
I thought doing something like
SELECT user_id
FROM user_assessment
WHERE video_id = '1' AND video_id = '2' AND video_id = '3'
but that clearly is incorrect.
Any help would be appreciated.
The key here is that the value checked in the HAVING clause (3) is equal to the number of elements contained within the IN grouping. This guarantees that all three videos are included.