I have a table with entries which has a DATE field. Each entry has a distinct date.
I’d like to select all the entries from the last month registred in the database. How?
I tried:
SELECT *
FROM registries
WHERE reg_date = DATE_FORMAT(MAX(reg_date), "%m")`
…without success
Based on OMG Ponies’ query with corrections:
Though the performance of the query wouldn’t be excellent, since it would operate the JOIN on two calculated values.
I believe it can still perform decently unless you start hitting millions of records.
On the other hand, you could probably run it faster by querying first for the MAX(reg_date)
And then injecting the result in a query:
With first_day as a place holder for the previous’ query result.
Provided you indexed reg_date, this should run pretty fast.
Note: LAST_DAY is a MySQL only function.