You need to join all tables together. Use the following to show all tasks assigned to John:
SELECT name,title
FROM users
JOIN assigned ON (user_id=users.id)
JOIN tasks ON (tasks.id=task_id)
WHERE name="John";
Use GROUP BY
and HAVING
to see all tasks that were only assigned to one user.
SELECT title
FROM tasks
JOIN assigned ON (task_id=id)
GROUP BY id
HAVING count(*) = 1;
In latter you don't necessarily need to know to who tasks were assigned to, just that they were only assigned to one user. Therefore you don't need to join users
table.
Update:
You can use the following to find tasks assigned to John alone:
SELECT name,title
FROM tasks
JOIN assigned ON (task_id=tasks.id)
JOIN users ON (user_id=users.id)
GROUP BY tasks.id
HAVING COUNT(*) = 1 and name="John";
This is possible due to two reasons:
- MySQL allows non-aggregated references in
HAVING
clause. COUNT(*)=1
forcesname
to be single value - i.e. you are not in a situation wherename
would have both 'John' and 'Mike'.