What is a mysql syntax for selecting all records enterd in the last 7 days from a table?
Demaestro
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
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
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
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
8:30 am on Jul 14, 2010 (gmt 0)
Sorry, I cannot solve your problem.
phex
6:34 pm on Jul 15, 2010 (gmt 0)
thanks a lot guys for the code. The now() does it for me, i appreciate.