Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select records from one week previous in mysql

I have data that has a date (Y-m-d H:i:s) column (the type is datetime). I'd like to select all records from 1 week before a date I enter (in the below example: 2011-09-17 00:00:00, but am having some trouble. Here's what I've got:

SELECT * FROM emails WHERE (DATE(date) = date_sub(date('2011-09-17 00:00:00'), 1 week))

What am I doing wrong? Thanks

like image 469
Andrew Samuelsen Avatar asked Sep 12 '11 17:09

Andrew Samuelsen


1 Answers

Try this, I like to stick with DATE_ADD and just use a negative value.

SELECT * FROM emails WHERE date >= DATE_ADD('2011-09-17 00:00:00', INTERVAL -1 WEEK)
like image 120
Adam Bailin Avatar answered Oct 11 '22 18:10

Adam Bailin