2016-09-03 1 views
-1

enter image description hereMySQL-Abfrage zwei Datum zu vergleichen, und es wird von einer Spalte zum anderen

ich einen Tisch pa_acc_status haben durchqueren, ich habe das Bild der Tabelle angebracht.

Ich möchte 2 Datum zu vergleichen (einer wird Benutzereingabe sein und andere werden Spaltenwert) und es wird von einer Spalte zu anderen in einer Schleife mit MYSQL durchlaufen.

Zum Beispiel: zuerst wird es prüfen, ob step_22_due_date Spalte nicht null ist und step_22_due_date < = USER_DATE wenn diese Ausfahrt wahr ist, dann für diese Reihe und weiter mit dem nächsten Zeile sonst für step_21_complete gleiche tut, dann step_21_due_date, step_1_complete dann

dh step_22_due_date ->step_21_complete ->step_21_due_date ->step_1_complete

ich die Ausgabe auf 2 Arten wollen

CASE 1: für USER_DATE = 2016-09-03

Erste Like this

id | step_1_complete | step_21_due_date | step_21_complete | step_22_due_date | user_id 
1 |  NULL  | 2016-03-12  | NULL    | NULL    | 1 
2 |  NULL  | 2016-03-12  | NULL    | NULL    | 1 
3 |  NULL  |  NULL  |  NULL  | 2016-04-15  | 2 
4 |  NULL  | 2016-03-12  | NULL    | NULL    | 2 
5 |  NULL  |  NULL  | 2016-03-12  | NULL    | 2 

Und zweitens eine, die die Zählung des Datums ist, die nicht null ist und group by user_id

step_1_complete | step_21_due_date | step_21_complete | step_22_due_date | user_id 
0   | 2   | 0   | 0   | 1 
0   | 1   | 1   | 1   | 2 

CASE 2: für USER_DATE = 2016-03-10

Zuerst Gefallen Sie diese

id | step_1_complete | step_21_due_date | step_21_complete | step_22_due_date | user_id 
1 |  2016-03-08 | NULL    | NULL    | NULL    | 1 
2 |  2016-03-08 | NULL    | NULL    | NULL    | 1 
3 |  2016-03-08 | NULL    | NULL    | NULL    | 2 
4 |  2016-01-03 | NULL    | NULL    | NULL    | 2 
5 | NULL   | 2016-03-08  | NULL    | NULL    | 2 

Und zweitens eine, die die Zählung des Datums ist, die nicht null ist und group by user_id

step_1_complete | step_21_due_date | step_21_complete | step_22_due_date | user_id 
2   | 0   | 0   | 0   | 1 
2   | 1   | 0   | 0   | 2 

Ich weiß es nicht möglich ist oder nicht MySQL verwenden, jede Hilfe oder einen Vorschlag werden sei hilfreich.

+0

sicher, dass es möglich ist. Sie könnten einen Cursor in einem gespeicherten Prozess schreiben – Drew

+1

Leistung wäre schrecklich – Drew

+0

Haben Sie in Betracht gezogen mit 'CASE WANN Schritt22_due_date IST NICHT NULL THEN Schritt_22_due_date WANN Schritt_21_Complete IST NICHT NULL DANN .... ELSE .. END' Sie können definitiv Ihre Logik mit schreiben so ein Ausdruck. https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#operator_case –

Antwort

0

Endlich habe ich die Lösung, meine Frage,

Für Datenliste

SET @USER_DATE = '2016-03-08'; 

SELECT * 
FROM 
    (SELECT `id`, 
      `user_id`, 
      `step_1_complete`, 
      NULL AS `step_21_due_date`, 
      NULL AS `step_21_complete`, 
      NULL AS `step_22_due_date` 
    FROM `pf_acc_status` 
    WHERE `step_1_complete` IS NOT NULL 
    AND `step_1_complete` <= @USER_DATE 
    AND (`step_21_due_date` IS NULL 
      OR step_21_due_date >= @USER_DATE) 
    UNION ALL SELECT `id`, 
        `user_id`, 
        NULL AS `step_1_complete`, 
        `step_21_due_date`, 
        NULL AS `step_21_complete`, 
        NULL AS `step_22_due_date` 
    FROM `pf_acc_status` 
    WHERE `step_21_due_date` IS NOT NULL 
    AND `step_21_due_date` <= @USER_DATE 
    AND (`step_21_complete` IS NULL 
      OR step_21_complete >= @USER_DATE) 
    UNION ALL SELECT `id`, 
        `user_id`, 
        NULL AS `step_1_complete`, 
        NULL AS `step_21_due_date`, 
        `step_21_complete`, 
        NULL AS `step_22_due_date` 
    FROM `pf_acc_status` 
    WHERE `step_21_complete` IS NOT NULL 
    AND `step_21_complete` <= @USER_DATE 
    AND (`step_22_due_date` IS NULL 
      OR step_22_due_date >= @USER_DATE) 
    UNION ALL SELECT `id`, 
        `user_id`, 
        NULL AS `step_1_complete`, 
        NULL AS `step_21_due_date`, 
        NULL AS `step_21_complete`, 
        `step_22_due_date` 
    FROM `pf_acc_status` 
    WHERE `step_22_due_date` IS NOT NULL 
    AND `step_22_due_date` <= @USER_DATE) AS `a` 
ORDER BY a.`id` ASC 

Für count Daten

SET @USER_DATE = '2016-03-08'; 

SELECT a.`user_id`, 
     count(a.`step_1_complete`) AS step_1_complete, 
     count(a.`step_21_due_date`) AS step_21_due_date, 
     count(a.`step_21_complete`) AS step_21_complete, 
     count(a.`step_22_due_date`) AS step_22_due_date 
FROM 
    (SELECT `id`, 
      `user_id`, 
      `step_1_complete`, 
      NULL AS `step_21_due_date`, 
      NULL AS `step_21_complete`, 
      NULL AS `step_22_due_date` 
    FROM `pf_acc_status` 
    WHERE `step_1_complete` IS NOT NULL 
    AND `step_1_complete` <= @USER_DATE 
    AND (`step_21_due_date` IS NULL 
      OR step_21_due_date >= @USER_DATE) 
    UNION ALL SELECT `id`, 
        `user_id`, 
        NULL AS `step_1_complete`, 
        `step_21_due_date`, 
        NULL AS `step_21_complete`, 
        NULL AS `step_22_due_date` 
    FROM `pf_acc_status` 
    WHERE `step_21_due_date` IS NOT NULL 
    AND `step_21_due_date` <= @USER_DATE 
    AND (`step_21_complete` IS NULL 
      OR step_21_complete >= @USER_DATE) 
    UNION ALL SELECT `id`, 
        `user_id`, 
        NULL AS `step_1_complete`, 
        NULL AS `step_21_due_date`, 
        `step_21_complete`, 
        NULL AS `step_22_due_date` 
    FROM `pf_acc_status` 
    WHERE `step_21_complete` IS NOT NULL 
    AND `step_21_complete` <= @USER_DATE 
    AND (`step_22_due_date` IS NULL 
      OR step_22_due_date >= @USER_DATE) 
    UNION ALL SELECT `id`, 
        `user_id`, 
        NULL AS `step_1_complete`, 
        NULL AS `step_21_due_date`, 
        NULL AS `step_21_complete`, 
        `step_22_due_date` 
    FROM `pf_acc_status` 
    WHERE `step_22_due_date` IS NOT NULL 
    AND `step_22_due_date` <= @USER_DATE) AS `a` 
GROUP BY a.`user_id` 
ORDER BY a.`id` ASC 
Verwandte Themen