Lewis Smith Lewis Smith - 1 month ago 8
MySQL Question

SQL get results up to 30 days ago

I have a coloumn called OrderDate and need to only return results where the orderdates are at most 30 days previous to today.

My code:

SELECT * FROM database WHERE Date(OrderDate) = SUBDATE(CURDATE(),30)


This doesn't seem to be working

Answer

Try:

SELECT *
FROM database
WHERE OrderDate >= SUBDATE(CURDATE(), INTERVAL 30 DAY)

Notes:

  • You have the wrong comparison, = instead of >=.
  • Second, you don't need to worry about the time component of OrderDate.
  • Third, use interval 30 day.