Ich habe diese zwei Tabellen:MySQL zwei Tabellen mit mehrere Eintrag auf demselben Tag, an dem zweiten Tisch beitreten
table1:
---------
UserID: Date: Day_status:
-----------------------------------
3004 2010-01-01 Normal
3004 2010-01-12 Normal
3004 2010-01-15 Ignore
3004 2010-01-18 Abnormal
4001 2010-01-09 Normal
4001 2010-01-11 Ignore
4001 2010-02-10 Normal
4001 2010-02-12 Abnormal
------------------------------------
table:2
-------
UserID: Date: Time Height
--------------------------------------------
3004 2010-01-01 08:01:02 35
3004 2010-01-01 09:01:12 32
3004 2010-01-01 18:01:02 40
3004 2010-01-02 07:01:02 20
3004 2010-01-02 08:01:02 30
3004 2010-01-02 10:01:02 10
3004 2010-01-08 07:01:02 20
3004 2010-01-08 08:01:02 30
3004 2010-01-08 10:01:02 10
3004 2010-01-12 07:01:02 30
3004 2010-01-12 09:01:02 50
3004 2010-01-12 18:01:02 30
3004 2010-01-15 07:01:02 30
3004 2010-01-15 09:01:02 50
3004 2010-01-15 18:01:02 30
3004 2010-01-18 07:01:02 30
3004 2010-01-18 09:01:02 50
3004 2010-01-18 18:01:02 30
4001 2010-01-08 07:01:02 30
4001 2010-01-08 08:01:02 30
4001 2010-01-08 09:01:02 40
4001 2010-01-08 13:01:02 30
4001 2010-01-09 07:01:02 30
4001 2010-01-09 08:01:02 30
4001 2010-01-09 09:01:02 40
4001 2010-01-11 08:01:02 30
4001 2010-01-11 09:01:02 30
4001 2010-01-11 18:01:02 30
4001 2010-01-12 08:01:02 30
4001 2010-01-12 09:01:02 30
4001 2010-01-12 18:01:02 30
4001 2010-01-13 08:01:02 30
4001 2010-01-13 09:01:02 30
4001 2010-01-13 18:01:02 30
4001 2010-02-12 08:01:02 30
4001 2010-02-12 09:01:02 30
4001 2010-02-12 09:01:02 30
-----------------------------------------------
Bitte denken Sie daran, dass in Tabelle 1: ein Benutzer mehr ‚s Datum hat (Benutzer 3004 mehr Datum hat). Und in Tabelle 2,
kann ein Benutzer mehrere 'Datum' haben und jedes Datum kann mehrere 'Zeit' haben. In Tabelle 2 hat der Benutzer 3004 3 verschiedene 'Zeitangaben für das Datum' 2011-01-01 'und so weiter.
Ich möchte diese beiden Tabellen verknüpfen, so dass
1) alle Daten aus table2 und nur die Day_status Daten aus Tabelle1 im Ergebnis enthalten ist.
2), wenn es kein ‚Day_status‘ ist für das Datum der table2 in Tabelle 1, der Day_status für dieses Datum im Ergebnis wird als ‚Normal‘ angezeigt (Anmerkung: Tabelle 2 hat mehr Datumseintrag als Tabelle 1)
3) der Eintrag mit Day_status = ‚Ignorieren‘ von table1 nicht
im Endergebnis sein wird wie folgtder Ausgang wird:
UserID: Date: Time Height Day_status_val
---------------------------------------------------------------
3004 2010-01-01 08:01:02 35 Normal
3004 2010-01-01 09:01:12 32 Normal
3004 2010-01-01 18:01:02 40 Normal
3004 2010-01-02 07:01:02 20 Normal
3004 2010-01-02 08:01:02 30 Normal
3004 2010-01-02 10:01:02 10 Normal
3004 2010-01-08 07:01:02 20 Normal
3004 2010-01-08 08:01:02 30 Normal
3004 2010-01-08 10:01:02 10 Normal
3004 2010-01-12 07:01:02 30 Normal
3004 2010-01-12 09:01:02 50 Normal
3004 2010-01-12 18:01:02 30 Normal
3004 2010-01-18 07:01:02 30 Abnormal
3004 2010-01-18 09:01:02 50 Abnormal
3004 2010-01-18 18:01:02 30 Abnormal
4001 2010-01-08 07:01:02 30 Normal
4001 2010-01-08 08:01:02 30 Normal
4001 2010-01-08 09:01:02 40 Normal
4001 2010-01-08 13:01:02 30 Normal
4001 2010-01-09 07:01:02 30 Normal
4001 2010-01-09 08:01:02 30 Normal
4001 2010-01-09 09:01:02 40 Normal
4001 2010-01-12 08:01:02 30 Normal
4001 2010-01-12 09:01:02 30 Normal
4001 2010-01-12 18:01:02 30 Normal
4001 2010-01-13 08:01:02 30 Normal
4001 2010-01-13 09:01:02 30 Normal
4001 2010-01-13 18:01:02 30 Normal
4001 2010-02-12 08:01:02 30 Abnormal
4001 2010-02-12 09:01:02 30 Abnormal
4001 2010-02-12 09:01:02 30 Abnormal
-----------------------------------------------
ich die folgende Abfrage mein Ergebnis verwendet für immer:
SELECT table2.UserID, table2.Date, table2.Time, table2.Height,
CASE
when table1.Day_status='Abnormal Day' then 'Abnormal Day'
when table1.Day_status='Normal Day' then 'Normal Day'
else 'Normal Day'
END as Day_status_val
FROM table2 LEFT JOIN table1
ON table2.UserID = table1.UserID and table1.Day_status !='Ignore'
Aber es gibt ein fehlerhaftes Ergebnis. Kann mir bitte jemand diesbezüglich helfen. Bitte schauen Sie sich die Daten in Tabelle1 und Tabelle2 und meine gewünschte Ausgabe an.
Entschuldigung für späte Antwort. Vielen Dank für die Hilfe. Ich werde es ausprobieren. – Tariq