如何在PostgreSQL中将同一表中的几列进行连接?

huangapple go评论55阅读模式
英文:

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.
如何在PostgreSQL中将同一表中的几列进行连接?

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?
如何在PostgreSQL中将同一表中的几列进行连接?

答案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

huangapple
  • 本文由 发表于 2023年7月20日 19:34:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定