Use Group By
and Having
.
SELECT
ul.user_id
count(1)
FROM user_states
GROUP BY ul.user_id
HAVING count(1) = 50
This will count the number of states that each user has and will only return the users that have 50 states. If you want to check a specific user, add a Where
clause. If user with id 11
doesn't have all 50 states, no rows will be returned.
SELECT
ul.user_id
count(1)
FROM user_states
WHERE ul.user_id = 11
GROUP BY ul.user_id
HAVING count(1) = 50