Jump to content

[SOLVED] Query rows this week


Canman2005

Recommended Posts

Hi all

 

I have a database table that has a field called `date`, the field is in the format YYYY-MM-DD

 

How could I write a query which would select all rows happening this week, so if today was a Tues, it would grab

 

Tues, Wed, Thurs, Fri, Sat, Sun

 

and if it was a Fri it would grab

 

Fri, Sat, Sun

 

Thanks very much everyone

 

Dave

Link to comment
Share on other sites

23/08/2009 is not the proper DATE data type format YYYY-MM-DD.

 

The WHERE clause I posted works, I just tested it -

 

$query = "SELECT * FROM table_name WHERE WEEK(CURDATE(),1) = WEEK(date,1) AND date >= CURDATE()";

 

That returned rows with today's date 2009-08-23 (Sunday of the current week) and it did not return rows with yesterday's or tomorrow's date. Temporarily setting the current date to yesterday, it returns rows that match yesterday and today as expected. Temporarily setting the current date to tomorrow (Monday of next week), it returns row that match Monday - Sunday of next week.

 

The only problem it has is that if you have a date with a future year in the same week as the current week, it will return those as well. You would need to add a test for YEAR(CURDATE()) = YEAR(date) to eliminate future years.

Link to comment
Share on other sites

Why write some slow parsed/tokenized/interpreted php code when you can just add the following to the query -

 

AND YEAR(CURDATE()) = YEAR(date)

 

Anything you can manage to do directly in a query will be a minimum of 10 times faster than if you do it using php code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.