Sebastian Rush
Sebastian Rush

Reputation: 538

MySQL getting last month instead of specific week

I have this part of my SQL-statement where I select the one week depending on the input date:

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]."')";

Now I want to select the last month (in this case September) instead of the user-specific week like this:

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)";

...but it seems not to work.

Upvotes: 0

Views: 49

Answers (1)

John Ruddell
John Ruddell

Reputation: 25872

the issue is you are passing an additional parameter to MONTH() I also don't think you need to do an interval on the YEAR (but you may want to not sure)

change WHERE MONTH(md.mydate,1) to WHERE MONTH(md.mydate)

Upvotes: 1

Related Questions