Welcome to WebmasterWorld Guest from 54.167.252.62

Forum Moderators: open

Message Too Old, No Replies

selecting only 7 days records from table

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

5+ Year Member



guys

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

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



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 Jul 13, 2010 (gmt 0)

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



^ ^ 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 Jul 13, 2010 (gmt 0)

5+ Year Member



Demaestro

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

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



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 Jul 14, 2010 (gmt 0)



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

5+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month