Sql 从多个表中获取记录

Sql 从多个表中获取记录,sql,postgresql,join,Sql,Postgresql,Join,我需要连接postgresql中三个表中的记录 表结构: 用户 Residence_id | Name | Mobile 1234 | A | 9876 23456 | B | 9786 Residence_id | Balance 1234 | 1000 4545 | 567 Residence_id | City | Country 1234 | Mumbai | India 0124

我需要连接postgresql中三个表中的记录

表结构:

用户

Residence_id | Name | Mobile

1234         | A    | 9876
23456        | B    | 9786
Residence_id | Balance

1234         | 1000
4545         | 567
Residence_id | City   | Country

1234         | Mumbai | India
0124         | London | UK
客户

Residence_id | Name | Mobile

1234         | A    | 9876
23456        | B    | 9786
Residence_id | Balance

1234         | 1000
4545         | 567
Residence_id | City   | Country

1234         | Mumbai | India
0124         | London | UK
地址

Residence_id | Name | Mobile

1234         | A    | 9876
23456        | B    | 9786
Residence_id | Balance

1234         | 1000
4545         | 567
Residence_id | City   | Country

1234         | Mumbai | India
0124         | London | UK
预期结果:

Residence_id | Name | Mobile | Balance | City   | Country

1234         | A    | 9876   | 1000    | Mumbai | India
23456        | B    | 9786   |         |        |
4545         |      |        | 567     |        |
0124         |      |        |         | London | UK

提前谢谢。

我认为您最好的选择是设置一个包含所有居住ID的居住表:

residence_id(PK):
1234
23456
4545
0124
此列可以用作其他3个表中的约束(外键)。要获得结果,您需要查询此表,并连接到其他3个表。像这样的方法应该会奏效:

 SELECT residence.residence_id, users.name, users.mobile, accounts.balance, address.city, address.country 
  FROM residence LEFT OUTER JOIN users ON residence.residence_id=users.residence_id
  LEFT OUTER JOIN accounts ON residence.residence_id=accounts.residence_id
  LEFT OUTER JOIN address ON residence.residence_id=address.residence_id

我认为您最好的选择是设置一个包含所有居住ID的居住表:

residence_id(PK):
1234
23456
4545
0124
此列可以用作其他3个表中的约束(外键)。要获得结果,您需要查询此表,并连接到其他3个表。像这样的方法应该会奏效:

 SELECT residence.residence_id, users.name, users.mobile, accounts.balance, address.city, address.country 
  FROM residence LEFT OUTER JOIN users ON residence.residence_id=users.residence_id
  LEFT OUTER JOIN accounts ON residence.residence_id=accounts.residence_id
  LEFT OUTER JOIN address ON residence.residence_id=address.residence_id