selecting records in mysql db from 1, 7, and 30 days ago with datetime and php

Viewed 40851

Im using the following query to target results that are exactly X days older than current time.

SELECT  *,
        DATE_FORMAT(datetime, '%m/%d/%Y') 
   FROM table 
  WHERE datetime BETWEEN SYSDATE() - INTERVAL 30 DAY 
                     AND SYSDATE() 
ORDER BY ID DESC

Problem is its returning data from current day if a record from exactly 30 days ago doesnt exist, as well as irrelevant data

is there a better way of doing this?

2 Answers
Related