Jump to content

Select articles from within a month of todays date?


Perad

Recommended Posts

$month = "2007-06-01"; // SQL date format YYYY-MM-DD

$result = mysql_query($query = "
     SELECT * FROM articles
     WHERE date_created BETWEEN '{$month}' AND last_day('{$month}')") or trigger_error(mysql_error()."<PRE>".$query."</PRE>", E_USER_ERROR);

If you always want only this month's posts, you can just do this:

SELECT * FROM articles WHERE MONTH(date_created) = MONTH();

 

If you want to get all of the articles one month from today, just do this:

SELECT * FROM articles WHERE date_created BETWEEN CURDATE() AND CURDATE() + INTERVAL 1 MONTH;

This is a little silly and pedantic but anyways:

 

SET @date_created = '2007-06-01';

SELECT benchmark(10000000, month(@date_created) = month(CURRENT_DATE));
# ~ 531ms

SELECT benchmark(10000000, @date_created BETWEEN CURRENT_DATE AND last_day(CURRENT_DATE));
# ~ 219ms

 

The month function appears to take some time.

 

version = 4.1.21-community-nt

oh yeah... but take a look at this...

 

SET @date_created = '2007-06-01';

SELECT benchmark(10000000, month(@date_created) = month(CURRENT_DATE));
# ~ 2719ms

SELECT benchmark(10000000, @date_created BETWEEN last_day(CURRENT_DATE - interval 1 month) AND last_day(CURRENT_DATE));
# - 297ms

 

I must have previously not run the SET line... *slaps head*

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.