Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL select rows from exactly 7 days ago

I'm completely stumped on this one, being trying for hours but with no success, hoping someone can help. Trying to build a cron script to run daily that returns the rows that are exactly 7 days older than the current date.

The thing is, my query is not returning anything. No error messges, nothing (I know there are entries in the DB from the last 7 days - we get about 7000 new entries a day, so they are there!) I've tried a SELECT * and echo out the edit date with success, so everything is working, apart from my SQL script.

The column I'm referencing (edit_date) is type 'datetime' formated with Y-m-d h-m-s. This column always has a datetime value assigned on both create and edit.

function get_ad_sql($table){     $sql = "SELECT                  *              FROM                  ".$table."              WHERE                  edit_date = DATE_SUB(NOW(), INTERVAL 7 DAY)             ";       return $sql; } 

And calling the function and 'trying' to echo the primary_key:

$sqlAng = get_ad_sql('angebote'); $result = mysql_query($sqlAng); while($row = mysql_fetch_array($result)){     echo $row['primary_key']; } 

I've tried every variation of DATE_SUB(NOW(), INTERVAL 7 DAY), including CURDATE(), DATE_FORMAT(edit_date, '%m/%d/%Y') that I could find on here and online, but couldn't get anything to work. Hope someone can help me!

like image 932
Tim Blackburn Avatar asked Oct 13 '11 15:10

Tim Blackburn


People also ask

How do I get last 7 days data in SQL query?

Here's the SQL query to get records from last 7 days in MySQL. In the above query we select those records where order_date falls after a past interval of 7 days. We use system function now() to get the latest datetime value, and INTERVAL clause to calculate a date 7 days in the past.

How do I get last 30 days records in SQL?

SELECT * FROM product WHERE pdate >= DATEADD(day, -30, getdate()).

How can I get specific date records in MySQL?

You can use DATE() from MySQL to select records with a particular date. The syntax is as follows. SELECT *from yourTableName WHERE DATE(yourDateColumnName)='anyDate'; To understand the above syntax, let us first create a table.

Does Datepart work in MySQL?

There is no DATEPART function in MySQL. Use MONTH(date_column) or EXTRACT(MONTH FROM date_column) instead.


2 Answers

It is very rare to get same datetime entries which gives date and time upto seconds. Therefore, for getting appropriate results we need to ignore the time part, and deal with date part, thus, using CURDATE() function.

You could do that ignoring the time part and compare with the date using following:

function get_ad_sql($table){     $sql = "SELECT                  *              FROM                  ".$table."              WHERE                  DATE(edit_date) = DATE_SUB(CURDATE(), INTERVAL 7 DAY)             ";       return $sql; } 
like image 175
Ghazanfar Mir Avatar answered Oct 04 '22 06:10

Ghazanfar Mir


NOW() returns DATETIME value, you should use a DATE function to get date without time, e.g. -

SELECT * FROM table WHERE edit_date = DATE_SUB(DATE(NOW()), INTERVAL 7 DAY); 

If type of edit_date field is DATETIME, then this field should be wrapped by DATE() function too -

SELECT * FROM table WHERE DATE(edit_date) = DATE_SUB(DATE(NOW()), INTERVAL 7 DAY); 
like image 27
Devart Avatar answered Oct 04 '22 07:10

Devart