从相同的 table 加入具有相同外键的 2 tables 但一个是一对多

Joining 2 tables which have the same foreign key from same table but one is a one to many

我想查看我酒店中某个特定会员的 activity。我想看看他们参观了哪些酒店房间。但是,如果他们访问了相同的酒店房间,我只想要该酒店房间的最近一次访问

customers
+--------+-------+-----------+
| cus_id | name  | driver_id |
+--------+-------+-----------+
|      1 | bob   |     11111 |
|      2 | james |     22222 |
|      3 | sam   |     33333 |
|      4 | billy |     44444 |
+--------+-------+-----------+


hotel_rooms (cus_id is the owner of the room)
+----------+------------+--------+
| hroom_id |    name    | cus_id |
+----------+------------+--------+
|        1 | small room |      3 |
|        2 | big room   |      1 |
+----------+------------+--------+

snapshots (when we detected the user in the room)
+-------------+----------+---------------------+
| snapshot_id | hroom_id |     date_added      |
+-------------+----------+---------------------+
|           1 |        1 | 2020-01-12 12:43:13 |
|           2 |        1 | 2020-01-13 17:23:53 |
|           3 |        2 | 2020-01-19 07:34:01 |
+-------------+----------+---------------------+

participants (who was also present in the same room at the particular time we detected the customer in the room)
+----------------+-------------+--------+
| participant_id | snapshot_id | cus_id |
+----------------+-------------+--------+
|              1 |           1 |      1 |
|              2 |           1 |      3 |
|              3 |           2 |      1 |
|              4 |           2 |      2 |
|              5 |           2 |      3 |
|              6 |           3 |      1 |
|              7 |           3 |      4 |
+----------------+-------------+--------+

参与者 table 基本上说的是:

查找 bob 的 activity 所需的结果:

+-------------+------------+-----------------+---------------------+-------------------------+
| snapshot_id |    name    | owner_driver_id |     date_added      | participants_driver_ids |
+-------------+------------+-----------------+---------------------+-------------------------+
|           2 | small room |           33333 | 2020-01-13 17:23:53 | 11111,22222,33333       |
|           3 | big room   |           11111 | 2020-01-19 07:34:01 | 11111,44444             |
+-------------+------------+-----------------+---------------------+-------------------------+

我不知道如何为所有者和参与者合并 driver_id

SELECT s.snapshot_id, hr.name, c.driver_id as owner_driver_id, MAX(s.date_added) as date_added, GROUP_CONCAT(p.driver_id) as participants_driver_ids
FROM hotel_rooms hr INNER JOIN
snapshots s
ON hr.hroom_id = s.hroom_id JOIN
participants p
ON s.snapshot_id = p.snapshot_id JOIN
customers c
ON hr.cus_id = c.cus_id
WHERE p.cus_id = 1
GROUP BY hr.cus_id
ORDER BY date_added

您可以通过首先找到 bob 所在的每个房间的最大快照日期,然后将其加入快照 table 以获得每个房间的最新快照来获得您想要的结果房间。然后你需要加入customers两次,一次获取房间所有者的名字,一次获取每个参与者的名字:

SELECT s.snapshot_id, 
       hr.name, 
       c1.driver_id AS owner_driver_id,
       md.max_date AS date_added,
       GROUP_CONCAT(c2.driver_id) AS participants_driver_ids 
FROM snapshots s
JOIN (
  SELECT hr.hroom_id, MAX(date_added) AS max_date
  FROM hotel_rooms hr
  JOIN snapshots s ON s.hroom_id = hr.hroom_id
  JOIN participants p ON p.snapshot_id = s.snapshot_id
  JOIN customers c ON c.cus_id = p.cus_id
  WHERE c.name = 'bob'
  GROUP BY hr.hroom_id, hr.name
) md ON md.hroom_id = s.hroom_id AND md.max_date = s.date_added
JOIN hotel_rooms hr ON hr.hroom_id = s.hroom_id
JOIN customers c1 ON c1.cus_id = hr.cus_id
JOIN participants p ON p.snapshot_id = s.snapshot_id
JOIN customers c2 ON c2.cus_id = p.cus_id
GROUP BY s.snapshot_id, hr.name, c1.driver_id, md.max_date
ORDER BY md.max_date DESC
LIMIT 15

输出:

snapshot_id     name        owner_driver_id     date_added              participants_driver_ids
2               small room  33333               2020-01-13 17:23:53     11111,33333,22222
3               big room    11111               2020-01-19 07:34:01     44444,11111

Demo on SQLFiddle