What's the best way to express this in one SQL query?
"Select a few random items that fall within x days of the newest item in the table."
I tried the following:
SELECT *
FROM table
HAVING `timestamp` >= SUBDATE(MAX(`timestamp`), INTERVAL 5 DAY)
ORDER BY RAND()
LIMIT 10
But this only gives me a single result, not 10. WHERE
instead of HAVING
doesn't cut it because of the use of MAX()
.