如何在单个 SQL 语句中查询 postgresql table 两次

How to query a postgresql table twice in a single SQL statement

我有两个 table。

af_fibrephase是非洲光纤网络项目列表,由关键字段组成:

af_organisation 是 table,它提供了有关 af_fibrephase 中引用的组织的更多详细信息。我对运营商和所有者组织都使用了一个 table,因为它们有时是同一件事,有时又不是。 af_organisation 的关键字段是:

我想进行查询,从 af_fibrephase 中提取项目并填充有关组织 table 的所有者和操作员的详细信息。通过示例,看起来像下面这样的东西应该有效,但它会产生连接错误。

SELECT af_fibrephase.* 
FROM af_fibrephase AS fibre
INNER JOIN af_organisation AS org1 on fibre.operator_id = org1.organisation_id
INNER JOIN af_organisation AS org2 on fibre.owner_id = org2.organisation_id

实际 table 可以在 af_fibrephase and af_organisation on https://carto.com

找到
SELECT fibre.*, 
       org1.name as org1_name,
       org2.name as org2_name
FROM af_fibrephase AS fibre
LEFT JOIN af_organisation AS org1 on fibre.operator_id = org1.organisation_id
LEFT JOIN af_organisation AS org2 on fibre.owner_id = org2.organisation_id

见上面的评论。使用 LEFT OUTER JOIN 和 af_organisation.

中的别名所需字段
SELECT fibre.*
    , org1.name AS operator_name
    , org1.web_url AS operator_web_url
    , org1.<etc> AS operator_<etc>
    , org2.name AS owner_name
    , org2.web_url AS owner_web_url
    , org2.<etc> AS owner_<etc>
FROM af_fibrephase AS fibre
LEFT OUTER JOIN af_organisation org1 on fibre.operator_id = org1.organisation_id
LEFT OUTER JOIN af_organisation org2 on fibre.owner_id = org2.organisation_id