postgresql 如何在postgres中对同一个表中的几个列进行连接?

xam8gpfp  于 2023-08-04  发布在  PostgreSQL
关注(0)|答案(2)|浏览(128)

我有table。该表有几个带有用户ID的列。只有一个表包含用户。

的数据
如果有一列,那么一切都很清楚,我们加入user_id = users.id,然后使用users.name。如果有多列呢?如何对每一列进行连接?

gopyfrb3

gopyfrb31#

可以使用不同的别名联接同一个表。

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

字符串

arknldoa

arknldoa2#

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;

字符串

相关问题