2014-07-26 76 views
0

我有一個查詢Mysql的問題,MySQL查詢STR_TO_DATE和DATE_ADD

SELECT orereparto.settimana AS 'Sett', (CASE giorno 
    WHEN 'lunedi' THEN STR_TO_DATE(orereparto.settimana,'%d,%m,%Y') 
    WHEN 'martedi' THEN DATE_ADD(STR_TO_DATE(orereparto.settimana,'%d,%m,%Y') + INTERVAL 1 DAY) 
    WHEN 'mercoledi' THEN DATE_ADD(STR_TO_DATE(orereparto.settimana,'%d,%m,%Y') + INTERVAL 2 DAY) 
    WHEN 'giovedi' THEN DATE_ADD(STR_TO_DATE(orereparto.settimana,'%d,%m,%Y') + INTERVAL 3 DAY) 
    WHEN 'venerdi' THEN DATE_ADD(STR_TO_DATE(orereparto.settimana,'%d,%m,%Y') + INTERVAL 4 DAY) 
    WHEN 'sabato' THEN DATE_ADD(STR_TO_DATE(orereparto.settimana,'%d,%m,%Y') + INTERVAL 5 DAY) 
    WHEN 'domenica' THEN DATE_ADD(STR_TO_DATE(orereparto.settimana,'%d,%m,%Y') + INTERVAL 6 DAY) 
    ELSE 0 
END) AS 'Giorno', reparti.nomerep AS 'Reparto', SUM(orereparto.orereps) AS 'Ore' FROM orereparto INNER JOIN smk ON orereparto.idsmk = smk.idsmk INNER JOIN reparti ON orereparto.idrep = reparti.idrep WHERE smk.nomesmk = 'Stalingrado' AND orereparto.settimana = '21/07/2014' GROUP BY reparti.nomerep,giorno ORDER BY reparti.nomerep ASC 

服務器回答我:

# 1064 - You have an error in your SQL syntax; check the manual That corresponds to your MySQL server version for the right syntax to use near ') WHEN' Wednesday 'THEN DATE_ADD (STR_TO_DATE (orereparto.settimana,'% d,% m,% Y ')' at line 3 

回答

0

DATE_ADD需要一個逗號interval語句之前:

SELECT orereparto.settimana AS 'Sett', (CASE giorno 
    WHEN 'lunedi' THEN STR_TO_DATE(orereparto.settimana,'%d,%m,%Y') 
    WHEN 'martedi' THEN DATE_ADD(STR_TO_DATE(orereparto.settimana,'%d,%m,%Y'), INTERVAL 1 DAY) 
    WHEN 'mercoledi' THEN DATE_ADD(STR_TO_DATE(orereparto.settimana,'%d,%m,%Y'), INTERVAL 2 DAY) 
    WHEN 'giovedi' THEN DATE_ADD(STR_TO_DATE(orereparto.settimana,'%d,%m,%Y'), INTERVAL 3 DAY) 
    WHEN 'venerdi' THEN DATE_ADD(STR_TO_DATE(orereparto.settimana,'%d,%m,%Y'), INTERVAL 4 DAY) 
    WHEN 'sabato' THEN DATE_ADD(STR_TO_DATE(orereparto.settimana,'%d,%m,%Y'), INTERVAL 5 DAY) 
    WHEN 'domenica' THEN DATE_ADD(STR_TO_DATE(orereparto.settimana,'%d,%m,%Y'), INTERVAL 6 DAY) 
    ELSE 0 
END) AS 'Giorno', reparti.nomerep AS 'Reparto', SUM(orereparto.orereps) AS 'Ore' 
FROM orereparto INNER JOIN 
    smk 
    ON orereparto.idsmk = smk.idsmk INNER JOIN 
    reparti 
    ON orereparto.idrep = reparti.idrep 
WHERE smk.nomesmk = 'Stalingrado' AND orereparto.settimana = '21/07/2014' 
GROUP BY reparti.nomerep,giorno 
ORDER BY reparti.nomerep ASC; 

您還可以將intervals添加到日期,只需說:

STR_TO_DATE(orereparto.settimana,'%d,%m,%Y') + INTERVAL 1 DAY 

但是你正在混合兩種語法。

+0

謝謝!爲我工作! –