我正在使用MySQL(5.7),我想加入2個表的基礎上一個ID和時間戳。MySQL JOIN不規則表
我的表是這樣的:
主表:
+--------+---------------------+-------------+
| UserId | Timestamp | Information |
+--------+---------------------+-------------+
| 5 | 2015-11-29 11:15:00 | 1 |
| 5 | 2015-11-29 11:30:00 | 1 |
| 5 | 2015-11-29 11:45:00 | 1 |
| 5 | 2015-11-29 12:00:00 | 1 |
| 5 | 2015-11-29 12:16:00 | 1 |
+--------+---------------------+-------------+
次要表:
+--------+---------------------+-------------+
| UserId | Timestamp | Information |
+--------+---------------------+-------------+
| 5 | 2015-11-29 11:00:00 | 2 |
| 5 | 2015-11-29 11:30:00 | 2 |
| 5 | 2015-11-29 11:45:00 | 2 |
| 5 | 2015-11-29 12:00:00 | 2 |
| 5 | 2015-11-29 12:01:00 | 2 |
| 5 | 2015-11-29 12:02:00 | 2 |
| 5 | 2015-11-29 12:03:00 | 2 |
| 5 | 2015-11-29 12:04:00 | 2 |
| 5 | 2015-11-29 12:05:00 | 2 |
| 5 | 2015-11-29 12:06:00 | 2 |
| 5 | 2015-11-29 12:07:00 | 2 |
| 5 | 2015-11-29 12:10:00 | 2 |
| 5 | 2015-11-29 12:15:00 | 2 |
| 5 | 2015-11-29 12:16:00 | 2 |
+--------+---------------------+-------------+
我想要做的是返回所有記錄(從一級和二級表) 。如果記錄不存在於一個或另一個表中,它將返回null。
每個表中的記錄可以以不同的時間間隔進入。
,我使用的查詢是:
SELECT UserId, Timestamp, PrimaryInformation, SecondaryInformation
FROM (
SELECT
UserId,
Timestamp
Information AS PrimaryInformation,
null AS SecondaryInformation
FROM Primary
WHERE UserId = 5 AND Timestamp BETWEEN '2015-11-29 11:00:00' AND '2015-11-29 12:20:00'
UNION
SELECT
UserId,
Timestamp
null AS PrimaryInformation,
Information AS SecondaryInformation
FROM Secondary
WHERE UserId = 5 AND Timestamp BETWEEN '2015-11-29 11:00:00' AND '2015-11-29 12:20:00'
) t
GROUP BY UserId, Timestamp, PrimaryInformation, SecondaryInformation
這將返回:
+--------+---------------------+---------------------+-----------------------+
| UserId | Timestamp | Primary Information | Secondary Information |
+--------+---------------------+---------------------+-----------------------+
| 5 | 2015-11-29 11:00:00 | null | 2 |
| 5 | 2015-11-29 11:15:00 | 1 | null |
| 5 | 2015-11-29 11:30:00 | 1 | null |
| 5 | 2015-11-29 11:30:00 | null | 2 |
| 5 | 2015-11-29 11:45:00 | 1 | null |
| 5 | 2015-11-29 11:45:00 | null | 2 |
| 5 | 2015-11-29 12:00:00 | 1 | null |
| 5 | 2015-11-29 12:00:00 | null | 2 |
| 5 | 2015-11-29 12:01:00 | null | 2 |
| 5 | 2015-11-29 12:02:00 | null | 2 |
| 5 | 2015-11-29 12:03:00 | null | 2 |
| 5 | 2015-11-29 12:04:00 | null | 2 |
| 5 | 2015-11-29 12:05:00 | null | 2 |
| 5 | 2015-11-29 12:06:00 | null | 2 |
| 5 | 2015-11-29 12:07:00 | null | 2 |
| 5 | 2015-11-29 12:10:00 | null | 2 |
| 5 | 2015-11-29 12:15:00 | null | 2 |
| 5 | 2015-11-29 12:16:00 | 1 | null |
| 5 | 2015-11-29 12:16:00 | null | 2 |
+--------+---------------------+---------------------+-----------------------+
這將返回重複的時間戳,而不是將它們合併。
,我需要它返回的路會是什麼樣子:
+--------+---------------------+---------------------+-----------------------+
| UserId | Timestamp | PrimaryInformation | SecondaryInformation |
+--------+---------------------+---------------------+-----------------------+
| 5 | 2015-11-29 11:00:00 | null | 2 |
| 5 | 2015-11-29 11:15:00 | 1 | null |
| 5 | 2015-11-29 11:30:00 | 1 | 2 |
| 5 | 2015-11-29 11:45:00 | 1 | 2 |
| 5 | 2015-11-29 12:00:00 | 1 | 2 |
| 5 | 2015-11-29 12:01:00 | null | 2 |
| 5 | 2015-11-29 12:02:00 | null | 2 |
| 5 | 2015-11-29 12:03:00 | null | 2 |
| 5 | 2015-11-29 12:04:00 | null | 2 |
| 5 | 2015-11-29 12:05:00 | null | 2 |
| 5 | 2015-11-29 12:06:00 | null | 2 |
| 5 | 2015-11-29 12:07:00 | null | 2 |
| 5 | 2015-11-29 12:10:00 | null | 2 |
| 5 | 2015-11-29 12:15:00 | null | 2 |
| 5 | 2015-11-29 12:16:00 | 1 | 2 |
+--------+---------------------+---------------------+-----------------------+
可能有人請點我在這個正確的方向。
謝謝:)
我猜ü左需要加入的,而不是工會。輔助表是否覆蓋了主要的所有時間戳? – 1000111
我玩過不同的JOIN,沒有運氣。 時間戳可以但不一定匹配在兩張表之間 –