Search code examples
sqlphpmyadmininnodb

Select rows from table inserted through the past week


I need to select all rows added in the last week in the database.

This is for a "Top 5" page which should show the most sold products over the past 7 days. I tried:

SELECT order_id
FROM orders
WHERE order_date BETWEEN DATE_ADD(week,-1,CURRENT_DATE) AND NOW() 

which returns this error:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1,CURRENT_DATE) AND NOW() LIMIT 0, 25' at line 1

Other things I have tried were simply the same query but with other syntax not working on this sql server.


Solution

  • you just need below

    SELECT order_id FROM orders 
    WHERE order_date >= NOW() - INTERVAL 1 WEEK