[英]Is it okay to have more than one foreign key from one table to the same table?
[英]Joining 2 tables which have the same foreign key from same table but one is a one to many
我想查看某個特定會員在我酒店的活動。 我想看看他們去過哪些酒店房間。 但是,如果他們訪問了相同的酒店房間,我只想要該酒店房間的最近一次訪問
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 |
+----------------+-------------+--------+
參與者表的內容基本上是:
snapshot_id=1
bob
和sam
在small room
。snapshot_id=2
, bob
, james
和sam
在small room
。snapshot_id=3
, bob
和billy
在big room
。 查找bob
活動的預期結果:
+-------------+------------+-----------------+---------------------+-------------------------+
| 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
所在的每個房間的最大快照日期,然后將其加入快照表以獲取每個房間的最新快照來獲得所需的結果。 然后你需要加入到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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.