如何加入 SQL 表,包括 none 现有关系

How to join SQL Tables including the none existing relations

我有一个 SQL 服务器加入问题。

我有 3 个 table:

最后,我想得到一家给定餐厅的结果 table,其中应列出这家餐厅拥有和不拥有的所有属性。所以我也需要输出空值。

| id     name         |    | idRes   idProp |  | id      prop  |
+---------------------+    +----------------+  +---------------+
| 01     restaurant-01|    | 1        1     |  | 1       wifi  |
| 02     restaurant-02|    | 1        2     |  | 2       pool  |
                           | 2        2     |  | 3       24/7  |
                           | 2        2     |  | 4       clean |

现在我想输出 ID 为 1 的餐厅及其拥有和不拥有的所有属性。

| id     name           idRes   idProp    id   prop  |
+----------------------------------------------------+
| 1      restaurant-1     1       1        1   wifi  |
| 1      restaurant-1     1       2        2   pool  |
| 1      restaurant-1     NULL    NULL     3   24/7  |
| 1      restaurant-1     NULL    NULL     4   clean |

我希望这甚至是可能的。提前谢谢大家。你真棒

您想知道与餐厅结合的所有属性是否存在关系。所以交叉连接餐厅和物业以及外部连接关系:

select *
from restaurant r
cross join property p
left join relation rp on rp.idres = r.id and rp.idprop = p.id
where r.id = 1;