views:

17

answers:

2

what is the different from this mysql query:

WHERE MONTH(date) = MONTH(CURRENT_DATE)

and this

WHERE date BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH) AND CURRENT_DATE()

i have tried both but i cant see the different.

A: 

The first one

WHERE MONTH(date) = MONTH(CURRENT_DATE)

will select rows where the month of the date column is the same as the current month (e.g. all rows for which the month of the date field is September.

The second one

WHERE date BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH) AND CURRENT_DATE()

will select rows for which the date field is between now and a month ago.

ring0
+1  A: 

For today's CURRENT_DATE, i.e.: 23-September-2010:

WHERE MONTH(date) = MONTH(CURRENT_DATE) is date also in September, i.e. between 01-September and 30-September of any year.

WHERE date BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH) AND CURRENT_DATE() is date within the last 30/31 days. If we're on 23rd, this will give you an interval between 23-August-2010 and 23-September-2010.

eumiro
@eumiro - the dates you gave for MONTH are slightly misleading - the year is not taken into account, hence _any_ september will match, not just 2010.
martin clayton
@martin - you're right, I will fix this.
eumiro