PostgreSQL多个左联接

时间:2018-07-11 09:35:56

标签: sql postgresql join left-join

我试图从一个表中获得一个列表,并在两列上建立连接。我认为如果我向您展示我想做的事情会更好,并希望它会有意义。

用户表:

username |  emailaddress  |                            socialkey                             |                                                       avatarpic                                
----------+----------------+------------------------------------------------------------------+------------------------------------------------------------------------------------------------
 Bobert   | bob@none.com   | 9784e946c53d44c975ee91625486d758fe630f176d44863080ec689ae3cd536a | https://craftedin.co/static/images/user/avatars/thumbs/59159be40a8c76.26091804.jpg
 Mikey    | mike@none.com  | 3bcd6c5f811ba06aa49c2df9504fe5416f35702b78bcdc15ecbd5137fabdca59 | http://cdn1-www.cattime.com/assets/uploads/gallery/siamese-cats-and-kittens-pictures/siamese-ca
 Mary     | mary@none.com  | f96197d19984b6f4a457ff0aed1c5bd222ffc2db51f30e4e8170ae02cb007ded | http://foopic.com
 Katya    | katya@none.com | 11ba72d57246c08ccbb9e201fb242a3f578d698e7d787d4a1aa08c8897b6cd80 | http://foopic.com

朋友表:

    usera     |     userb      
---------------+----------------
 mike@none.com | bob@none.com
 mary@none.com | bob@none.com
 bob@none.com  | katya@none.com

我的查询使用了用户 socialkey 并进行了查找。不幸的是,这给了我不好的结果。我想使用用户 socialkey

with Sfriends as (
   select * 
   from friends 
   where usera = (select emailaddress 
                  from users 
                  where socialkey = '9784e946c53d44c975ee91625486d758fe630f176d44863080ec689ae3cd536a')) 
     or (userb = (select emailaddress 
                  from users 
                  where socialkey = ('9784e946c53d44c975ee91625486d758fe630f176d44863080ec689ae3cd536a')))
), socialkeys as (
  select socialkey,username,emailaddress,avatarpic 
  from users
) 
select Sfriends.usera, socialkey , avatarpic, Sfriends.userb,socialkey 
from Sfriends as foo 
   left join Socialkeys on foo.usera = socialkeys.emailaddress 
   left join Sfriends on foo.userb = socialkeys.emailaddress;

这就是我想要的:

user-namea |     usera     |                          userasocialkey                          | usera-avatar-pic | user-nameb |     userb      |                          userbsocialkey                          |     userb-avatar-pic     
----------+---------------+------------------------------------------------------------------+------------------+----------+----------------+------------------------------------------------------------------+------------------
 Mikey    | mike@none.com | 3bcd6c5f811ba06aa49c2df9504fe5416f35702b78bcdc15ecbd5137fabdca59 | usera-avatar-pic | Bobert    | bob@none.com   | 9784e946c53d44c975ee91625486d758fe630f176d44863080ec689ae3cd536a | userb-avatar-pic
 Mary     | mary@none.com | f96197d19984b6f4a457ff0aed1c5bd222ffc2db51f30e4e8170ae02cb007ded | usera-avatar-pic | bobert    | bob@none.com   | 9784e946c53d44c975ee91625486d758fe630f176d44863080ec689ae3cd536a | userb-avatar-pic
 Bobert   | bob@none.com  | 9784e946c53d44c975ee91625486d758fe630f176d44863080ec689ae3cd536a | usera-avatar-pic | Katya     | katya@none.com | 11ba72d57246c08ccbb9e201fb242a3f578d698e7d787d4a1aa08c8897b6cd80 | userb-avatar-pic

如果愿意,我愿意将socialkey插入到friends表中。

1 个答案:

答案 0 :(得分:1)

尝试两次加入users,一次加入usera,一次加入userb,一次加入friends

SELECT ua.username,
       ua.emailaddress,
       ua.socialkey,
       ua.avatarpic,
       ub.username,
       ub.emailaddress,
       ub.socialkey,
       ub.avatarpic
       FROM friends f
            INNER JOIN users ua
                       ON ua.emailaddress = f.usera
            INNER JOIN users ub
                       ON ub.emailaddress = f.userb
       WHERE '9784e946c53d44c975ee91625486d758fe630f176d44863080ec689ae3cd536a' IN (ua.socialkey,
                                                                                    ub.socialkey);