Getting records from previous dates

[embed=425,349]$query = “(SELECT * FROM database WHERE fldDate > DATE_SUB(DATE(NOW()), INTERVAL 1 MONTH) ORDER BY fldDate DESC LIMIT 4)”;

$query = “(SELECT * FROM database WHERE fldDate > DATE_SUB(DATE(NOW()), INTERVAL 14 DAYS) ORDER BY fldDate DESC LIMIT 4)”;

$query = “(SELECT * FROM database WHERE fldDate > DATE_SUB(DATE(NOW()), INTERVAL 7 DAYS) ORDER BY fldDate DESC LIMIT 4)”;[/embed]

Im a little confused here. It’s getting the records by the recent dates, such as today for example, instead of the records from 1 month ago, 14 days ago and 7 days ago. I need the records from those days but 4 records from each. What is the issue here?

The three queries you posted gets all the records greater than a certain date…

First query: Gets all records greater then 30 days ago
Second Query: Gets all records greater then 14 days ago
Third Query: Gets all records greater then 7 days ago.

If you want specific records from a specific data change the > to an equal sign.

$query = “(SELECT * FROM database WHERE fldDate = DATE_SUB(DATE(NOW()), INTERVAL 1 MONTH) ORDER BY fldDate DESC LIMIT 4)”;

Thanks for the reply.

I have changed them to ‘=’ but it doesn’t display anything. It shows a blank page.

Maybe there are no orders with that specific date?

This DATE_SUB(DATE(NOW()), INTERVAL 1 MONTH) is going to return an exact date and time…

You’ll need to format both sides to exclude the time

http://www.w3schools.com/sql/func_date_format.asp

Thank you very much it seems to work now. Your right.

Sponsor our Newsletter | Privacy Policy | Terms of Service