Sunday, February 20, 2022

[FIXED] Select rows from table inserted through the past week

Issue

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


Answered By - Zaynul Abadin Tuhin

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.