skip to Main Content

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.

2

Answers


  1. you just need below

    SELECT order_id FROM orders 
    WHERE order_date >= NOW() - INTERVAL 1 WEEK
    
    Login or Signup to reply.
  2. If you are using Mysql try this to get last weeks data

    SELECT order_id FROM orders WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
    AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search