homepage Welcome to WebmasterWorld Guest from 54.166.111.111
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

    
selecting only 7 days records from table
phex




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

guys

What is a mysql syntax for selecting all records enterd in the last 7 days from a table?

 

Demaestro




msg:4168722
 6:49 pm on Jul 12, 2010 (gmt 0)

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

If yes, in what data type? DATE, DATETIME, STRING?

rocknbil




msg:4168857
 12:35 am on Jul 13, 2010 (gmt 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.

phex




msg:4169404
 6:58 pm on Jul 13, 2010 (gmt 0)

Demaestro

Yes i am storing the dates and the field is time and its data type is timestamp.

Demaestro




msg:4169409
 7:04 pm on Jul 13, 2010 (gmt 0)

This should be what you need then

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

youngflower




msg:4169860
 8:30 am on Jul 14, 2010 (gmt 0)

Sorry, I cannot solve your problem.

phex




msg:4170942
 6:34 pm on Jul 15, 2010 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved