eaglesphan
IS-IT--Management
I would like to write a SELECT statement where I can get the last 30 days worth of press releases. Reading the manual I found this code example:
This is close to what I'm looking for but not quite. I have solved this problem in PHP using two variables for the current date and current date minus a month and then using WHERE date BETWEEN $var1 AND $var2. But I'd like to be able to get MySQL to do all the work and not PHP. My problem is I get messed up if the month is January and subtracting 1 gives me zero, not the 12 I need. Is it possible to do this using functions rather than variables? Thanks.
Code:
SELECT name, birth FROM pet
WHERE MONTH(birth) = MOD(MONTH(CURDATE()), 12) + 1;
This is close to what I'm looking for but not quite. I have solved this problem in PHP using two variables for the current date and current date minus a month and then using WHERE date BETWEEN $var1 AND $var2. But I'd like to be able to get MySQL to do all the work and not PHP. My problem is I get messed up if the month is January and subtracting 1 gives me zero, not the 12 I need. Is it possible to do this using functions rather than variables? Thanks.