MySQL上个月而非特定周

时间:2014-09-01 15:00:11

标签: php mysql sql date datetime

我有我的SQL语句的这一部分,我根据输入日期选择一周:

SELECT DATE_FORMAT(md.`mydate`, '%d.%m.%Y'),
...
DAYNAME(md.`mydate`) FROM `mydates` md
LEFT JOIN (SELECT * FROM `import` 
    WHERE `Zeitspanne` != 'Anwesenheit' 
    AND `Person` = '".mysql_real_escape_string($expName)."') g 
    ON (md.`mydate` =  STR_TO_DATE(g.`Datum`, '%d.%m.%Y') )
WHERE WEEK(md.`mydate`,1) = WEEK('".$getStart[2].$d.$getStart[1].$d.$getStart[0]."',1)
AND YEAR(md.`mydate`) = YEAR('".$getStart[2].$d.$getStart[1].$d.$getStart[0]."')";

现在我想选择上个月(在本例中为9月)而不是像这样的用户特定周:

LEFT JOIN (
    SELECT * FROM `import` 
    WHERE `Zeitspanne` != 'Anwesenheit' 
    AND `Person` = '".mysql_real_escape_string($expName)."') g 
    ON (md.`mydate` =  STR_TO_DATE(g.`Datum`, '%d.%m.%Y') )
WHERE MONTH(md.`mydate`,1) = MONTH('".$getStart[2].$d.$getStart[1].$d.$getStart[0]."' - INTERVAL 1 MONTH)) 
AND YEAR(md.`mydate`) = YEAR('".$getStart[2].$d.$getStart[1].$d.$getStart[0]."' - INTERVAL 1 MONTH)";

......但似乎没有用。

1 个答案:

答案 0 :(得分:1)

问题是你要将一个额外的参数传递给MONTH() 我也认为你不需要在YEAR上做一段时间(但你可能不想确定)

WHERE MONTH(md.mydate,1)更改为WHERE MONTH(md.mydate)