【发布时间】:2021-02-03 07:15:13
【问题描述】:
我正在寻找一种将表格的结果连接到一行的方法。
我有 4 张桌子;
供应商表
+--+----------------+----------------+
|id|name |hook_name |
+--+----------------+----------------+
|1 |724 |724 |
|2 |Air |air |
|3 |Akustik |akustik |
|4 |Almira |almira |
+--+----------------+----------------+
供应商办公室;
(label 列代表接送字符串)
+---+-----------+----------+------------+
|id |supplier_id| zip_code | label |
+---+-----------+----------------+------+
|95 |24 |25325 | 344 | <- supplier_id 24 has office location 77,98 (label pickup)
|96 |24 |9535 | 93 | <- same. only label different (label dropoff)
|97 |1 |2858 | 95 |
|98 |1 |50285 | 954 |
|99 |1 |10094 | 24 |
|100|1 |4353 | 59 |
+---+-----------+----------------+------+
办公地点(数据透视表)
+------------------+-----------+
|supplier_office_id|location_id|
+------------------+-----------+
|95 |77 | <- location I want to concatenate `supplier_id = 24` (istanbul)
|96 |98 | <- location I want to concatenate `supplier_id = 24` (london)
|97 |77 |
|98 |77 |
+------------------+-----------+
地点
+---------------+
|id |name |
+---------------+
|77 |istanbul |
|96 |berlin |
|97 |newyork |
|98 |london |
+---------------+
我想找到给定位置信息的办公室。 我还没有设法创建关于标签的自定义列。 如果我想访问位置 1 和 2 的办公室信息,我想得到这样的东西;
+---------------+------------+----------------+
| supplier_id | pickup_label | dropoff_label |
+-------------+--------------+----------------+
| 95 | 344 | 93 |
+-------------+--------------+----------------+
我现在已经能够使用我的 Postgresl SQL 做到这一点。
SELECT supplier_offices.id,
supplier_offices.supplier_id
FROM "supplier_offices"
INNER JOIN suppliers on supplier_offices.supplier_id = suppliers.id
INNER JOIN office_locations on supplier_offices.id = office_locations.supplier_office_id
AND ("office_locations"."location_id" IN (77, 98)
【问题讨论】:
标签: sql postgresql join