Ich benutze MySQL (5.7), und ich möchte 2 Tabellen Basen auf einer ID und Timestamp verbinden.MySQL JOIN unregelmäßige Tabellen
Meine Tabellen wie folgt aussehen:
Primary Table:
+--------+---------------------+-------------+
| 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 |
+--------+---------------------+-------------+
Secondary Tabelle:
+--------+---------------------+-------------+
| 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 |
+--------+---------------------+-------------+
Was ich alle Datensätze tun möchte, ist das Rück (von primären und sekundären Tabellen) . Wenn der Datensatz in der einen oder anderen Tabelle nicht vorhanden ist, wird null zurückgegeben.
Die Datensätze in jeder Tabelle können in unterschiedlichen Zeitintervallen eingehen.
Die Abfrage, die ich benutze ist:
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
Das gibt:
+--------+---------------------+---------------------+-----------------------+
| 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 |
+--------+---------------------+---------------------+-----------------------+
Diese duplizierten Zeitstempel zurück, anstatt sie zu verschmelzen.
Die Art und Weise, dass ich es brauchen würde zurückkehren wie folgt aussehen:
+--------+---------------------+---------------------+-----------------------+
| 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 |
+--------+---------------------+---------------------+-----------------------+
Könnte jemand bitte diese mich in die richtige Richtung für Punkt.
Thanks :)
I u links müssen erraten beitreten statt Vereinigung. Umfasst die Sekundärtabelle alle Zeitstempel in der Primärdatenbank? – 1000111
Ich hatte ein Spiel mit verschiedenen JOINs ohne Glück. Die Zeitstempel konnten, aber nicht unbedingt übereinstimmen zwischen den 2 Tabellen –