Mysql - select rows which have linked values

V-K :

Sorry for the bad title, do not know how to describe it in one sentence.

There is the table:

id, user_id, task_id

I need to select user_ids which have records with several task_id Kind of pseudocode

Select user_id from tasks wherehas task_id = 1 and task_id = 2

Jeremy Harris :

As they say, there are many ways to skin a cat. Here is one:

To get a list of users that have multiple tasks assigned, and (at minimum) they have task_id 1 AND task_id 2:

SELECT 
   user_id
FROM user_tasks // or whatever your table is called 
WHERE task_id in (1, 2)
GROUP BY user_id
HAVING COUNT(DISTINCT task_id) = 2

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=194380&siteId=1