I have a table.
The table has several columns with user ids.
There is only one table with users.
If there was one column, then everything is clear, we do join user_id = users.id and then use users.name.
What if there are multiple columns? How can I do a join on each of the columns?
SELECT ua.name as adduser,
ue.name as edituser,
ud.name as deleteuser
FROM Table t
JOIN Users ua on t.adduser_id = ua.id
JOIN Users ue on t.edituser_id = ue.id
JOIN Users ud on t.deleteuser_id = ud.id;
You can join the same table using different aliases.
select adduser.name as adduser,
edituser.name as edituser,
deluser.name as deluser
from table as t
join users as adduser
on t.adduser_id = adduser.id
join users as edituser
on t.edituser_id = edituser.id
join users as deluser
on t.deluser_id = deluser.id
2
Answers
You can join the same table using different aliases.