Welcome to WebmasterWorld Guest from 50.17.117.221

Forum Moderators: open

Message Too Old, No Replies

selecting only 7 days records from table

     
6:06 pm on Jul 12, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 19, 2008
posts:49
votes: 0


guys

What is a mysql syntax for selecting all records enterd in the last 7 days from a table?
6:49 pm on July 12, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 15, 2003
posts:2606
votes: 0


It depends. Are you storing the dates that a record is entered on?

If yes, in what data type? DATE, DATETIME, STRING?
12:35 am on July 13, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


^ ^ That would be important. :-) If it's a datetime (or timestamp) field, use now() instead of curdate(), but for date format, if you want to include today,

select * from table where datefield >= date_sub(curdate(), interval 6 day) and datefield <= curdate();

If you want the previous 7 days NOT including today,

select * from table where datefield >= date_sub(curdate(), interval 7 day) and datefield < curdate();

If it's varchar, you have a problem and it has to be done in your programming.
6:58 pm on July 13, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 19, 2008
posts:49
votes: 0


Demaestro

Yes i am storing the dates and the field is time and its data type is timestamp.
7:04 pm on July 13, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 15, 2003
posts:2606
votes: 0


This should be what you need then

select * from table where datefield >= date_sub(curdate(), interval 6 day) and datefield <= curdate();
8:30 am on July 14, 2010 (gmt 0)

New User

5+ Year Member

joined:July 14, 2010
posts:1
votes: 0


Sorry, I cannot solve your problem.
6:34 pm on July 15, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 19, 2008
posts:49
votes: 0


thanks a lot guys for the code. The now() does it for me, i appreciate.