2017-04-17 4 views
0

Ich habe ein SELECT Ergebnis, das Spalten mit Datum enthalten. Dies ist einige Daten aus dem Ergebnis:Wie wählt man ein Datum "span" aus dieser Tabelle?

ID Name status   start_date  end_date 
A1 John Planning  2017-03-03  2017-03-05 
A1 John Realizing  2017-03-05  2017-04-05 
A1 John Finishing  2017-04-05  2017-04-15 
A1 John Launching  2017-04-15  2017-04-16 
A2 Lisa Planning  2017-03-09  2017-03-13 
A2 Lisa Realizing  2017-03-13   NULL 

Ich mag würde den Fortschritt Status von planning -> Realizing -> Finishing -> Launching für jeden ID kennen.

Ich habe diese Abfrage verwendet, aber ich weiß nicht, ob es zuverlässig und schnell genug sein wird. Die reale Tabelle enthält tausend Werte.

SELECT x.id, x.name, CONCAT_WS(' to ', MIN(x.start_date), MAX(x.end_date)) AS progress FROM (SELECT * FROM team_project WHERE ID = 'A1')x

Erwartete Ausgabe:

Name    Progress    total 
John  2017-03-03 to 2017-04-16  44 days 
Lisa  2017-03-09 to NOW    - 
+0

Verwenden Sie eine PHP-Seite? –

+0

@Brijesh Ja, ich benutze PHP. Aber ich hätte gerne das erwartete Ergebnis in mysql, wenn möglich. – Vahn

+0

Kann ich Ihren PHP-Code sehen? –

Antwort

0

ich mit group by denken können Sie diese

SELECT id, name, CONCAT_WS(' to ', MIN(start_date), MAX(end_date)) AS progress 
FROM team_project group by ID 
+0

Danke für Ihre Antwort. Wie fügt man 'NOW' als Text hinzu, wenn das end_date einen 'NULL' Wert hat? – Vahn

+0

'ifnull (MAX (end_date), CURDATE())' – ashkufaraz

+0

Wenn diese Antwort richtig ist, akzeptieren Sie sie bitte – ashkufaraz

0

Hier ist eine Abfrage ohne GROUP ist BY

SELECT 
    tp1.`name`, 
    CONCAT(tp1.start_date, ' to ', 
     IF(tp2.end_date IS NULL, 'NOW', tp2.end_date)) AS Progress, 
    DATEDIFF(IF(tp2.end_date IS NULL, NOW(), tp2.end_date),tp1.start_date) AS total 
FROM team_project tp1 
LEFT JOIN team_project tp2 ON tp1.id = tp2.id AND tp2.status 'Realizing' 
WHERE tp1.status ='Planning'; 
Verwandte Themen