SELECT
title,
description,
status,
task.user_id AS creatorUserId,
(SELECT user.nickname FROM user WHERE user.id = task.user_id) AS creatorUserNickname,
GROUP_CONCAT(user.id, "," ,user.nickname SEPARATOR ";") as responsibleUsers
FROM task JOIN task_user ON task.id = task_user.task_id
JOIN user ON task_user.user_id = user.id
GROUP BY title, description, status, creatorUserId, creatorUserNickname;
The query above works, but I wonder if it's possible to get the same result without a subquery.
I have a task table with user_id column for the user who created the task, and a user table with user data, like nickname.
Then there's a task_user table maping users assigned to the task.
My confusion lies on how to get different nicknames: one corresponding to task.user_id, the creator, and the others from task_user.user_id, the assignees.
The query below does't work, with creatorUserId and creatorUserNickname being repeated on responsibleUsers field, resulting in multiple rows for a single task.
SELECT
title,
description,
status,
task.user_id AS creatorUserId,
user.nickname AS creatorUserNickname,-- this user is referenced in task.user_id
GROUP_CONCAT(user.id,"," ,user.nickname SEPARATOR ";") as responsibleUsers -- these in task_user.user_id
FROM task JOIN task_user ON task.id = task_user.task_id
JOIN user ON task_user.user_id = user.id OR task.user_id = user.id
GROUP BY title, description, status, creatorUserId, creatorUserNickname;
Is there a way to select different user.nickname based on which table is referencing their id ?
Many thanks!