英文:
How to make join for few columns in same table in postgres?
问题
我有一张表。
这张表有几列用户ID。
只有一个包含用户的表。
如果只有一列,那么一切都很清楚,我们可以使用 user_id = users.id 进行连接,然后使用 users.name。
但如果有多列呢?如何在每列上进行连接?
英文:
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?
答案1
得分: 1
SELECT语句中的内容已翻译如下:
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;
英文:
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;
答案2
得分: 1
你可以使用不同的别名连接相同的表格。
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论