Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subtract one to the month of a date format in mysql? [duplicate]

Tags:

mysql

Assuming I have a select statement:

SELECT COUNT(*), wedding, DATE_FORMAT(weddate, '%y %m %d') FROM weddingtable

I want to make it such that I could subtract 1 from the month it would return: for example:

SELECT COUNT(*), wedding, DATE_FORMAT(weddate, '%y %m-1 %d') FROM weddingtable

It appears to just append "-1" to the month integer instead of actually subtracting one from the month. How do I accomplish this? (in other words, where it would normally return 5 in place of %m, it would return 4, etc.)

Note: For January, I would expect in place of %m, it would be "0".

like image 667
Rolando Avatar asked Jun 05 '13 17:06

Rolando


1 Answers

Your solution can be derived from: Subtract month and day mysql

e.g. (DATE_SUB(curdate(), INTERVAL 1 MONTH)

SELECT COUNT(*), wedding, DATE_SUB(weddate,INTERVAL 1 MONTH) FROM weddingtable
like image 132
Menelaos Avatar answered Oct 30 '22 20:10

Menelaos