Raphaël
Raphaël

Reputation: 1141

Mysql select and date >= current date - 3 weeks

I have a select query that retrieves all entries that are 3 weeks old.

I want to know if there is an expression like where date> Expr (CURDATE () - 3 WEEKS) or if I must first make calculations of differences in my php script.

The format of my date is a timestamp like that : 2010-06-21 16:59:59

Sincerely,

Upvotes: 3

Views: 6539

Answers (3)

Vinayak Mahadevan
Vinayak Mahadevan

Reputation: 191

you can use the following line of code

where date>DATE_SUB(curdate(),INTERVAL 3 WEEK);

Upvotes: 7

ajreal
ajreal

Reputation: 47331

Use the actual input of timestamp is better,
as it allow query cache to take effect

From :- http://www.dangrossman.info/2007/04/26/mysql-tuning-disable-query-cache-on-frequently-updated-databases/

Queries that contain non-deterministic functions aren’t cached. That includes CURDATE(), RAND(), or any other function where the output isn’t always the same.

From documentation :- http://dev.mysql.com/doc/refman/5.1/en/query-cache-operation.html

Upvotes: 2

Anthony Grist
Anthony Grist

Reputation: 38345

You can use the DATEDIFF() function like so: WHERE DATEDIFF(CURDATE(), date) = 21.

Upvotes: 5

Related Questions