Off the top of my head, I'd say use the
DATE_SUB(date,INTERVAL expression type) Function (see
manual page
http://www.mysql.com/doc/en/Date_and_time_functions.html
for details)

For example:

SELECT * FROM myTable WHERE date_field >=
DATE_SUB('2003-10-19', INTERVAL 7 DAY);

To get the records for the past 7 days from the
current day, use the NOW() function:

SELECT * FROM myTable WHERE date_field >=
DATE_SUB(NOW(), INTERVAL 7 DAY)

This assumes your date_field is of type DATE. If it is
of type DATETIME, you will need to consider if you
want records that are exactly within a 1 week time
frame (i.e. to the minute) or within a 1 week time
frame based on the day.

For example, if the current time is 2003-10-19 19:25
and you use this syntax:

SELECT * FROM myTable WHERE date_field >=
DATE_SUB(NOW(), INTERVAL 7 DAY)

A record with a datetime of "2003-10-12 19:00:00" is
NOT returned since it is older (by 25 minutes) than 7
days. However, if you change the syntax to limit the
calculation to year, month and day, such as:

SELECT * FROM myTable WHERE date_field >=
DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 7 DAY),
'%Y-%m-%d')

All records from "2003-10-12 00:00:00", including the
one from "2003-10-12 19:00:00" is returned.

I haven't used date & time functions extensively so
there may be a more elegant way that I am not familiar
with. If so, someone else, please chime in.

-Mark

--- Developer <[EMAIL PROTECTED]> wrote:
> 
> How can I pull all records within a 1 week time
> frame? So if I enter
> 2003-10-19, how can I write a query that will get
> all records from
> 2003-10-12 - 2003-10-19.
> 
> Thanks in advance. 
> 
> 
> 
> -- 
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:   
>
http://lists.mysql.com/[EMAIL PROTECTED]
> 


__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to