Mysql Find users that are part of two differnt projects

39 views Asked by At

I'm searching my submission fields for users that signed up for two different projects. This is what I have, that is not working correctly. Any help would be great!

SELECT 
    user_id, COUNT(*) 
FROM submissions 
WHERE 
    project_id = 125 
    or project_id = 81 
group by 
    user_id 
HAVING COUNT(*) >= 2

So to clarify, I want to know what users have a submission from project_id 81 AND project_id 125. Each of the submissions

1

There are 1 answers

0
fthiella On

The right sintax is this one, you're missing a *

SELECT
  user_id, COUNT(*)
FROM
 submissions
WHERE
 project_id = 125 or project_id = 81
GROUP BY
  user_id
HAVING
  COUNT(*) >= 2

in case a user can submit the same project multiple times, it's better to write your HAVING condition like this:

HAVING COUNT(DISTINCT project_id)>=2

so we can be sure that it will match two different distinct projects, and not just one project submitted multiple times