1

I have two tables user and task as follows:

user:

enter image description here

task:

enter image description here

I need to query to get all the user ids of the task id whose user is 2 and the output table should be: enter image description here

Can anybody help me with the query? Thanks in advance.

1 Answer 1

2

Use the GROUP_CONCAT function:

SELECT 
    t.task_id AS task_id,
    GROUP_CONCAT(t.user_id) AS user_id,
    GROUP_CONCAT(u.username) AS user
FROM task t
JOIN user u ON t.user_id = u.id
WHERE EXISTS (
   SELECT 1 FROM task
   WHERE task.task_id = t.task_id AND task.user_id = 2
)
GROUP BY t.task_id
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. It worked as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.