I am looking for the right query to retrieve orders that are 3 calendar years old. Example: It is October 18, 2023, and therefore I want to collect all orders from January 1, 2021 until October 18, 2023.
SELECT *
FROM ps_orders o
WHERE o.date_add >= DATE_ADD(NOW(),INTERVAL -3 YEAR);
I tried to do it like this, but it recovers 3 whole years, but I want it to be calendar.
Do you have a solution ?
2
Answers
You must use an intervall for example with
BETWEEN
The function MAKEDATe works like below
will return
You will need to truncate the lower bound of your date to the first of January, three years ago:
Caveat there may be a function which does this in MySQL, although I am not aware of it.