On my blog, I want to display the all the posts from the last month. But if that is less than 10 posts, I want to show the ten most recent posts (in other words, there should never be less than 10 posts on the front page). I am wondering if there is a way to do this in a single query?
Currently, I first run this query:
select count(*) from posts where timestamp > ($thirty_days_ago)
order by timestamp desc
If that count is greater than or equal to 10:
select * from posts where timestamp > ($thirty_days_ago)
order by timestamp desc
Otherwise:
select * from posts order by timestamp desc limit 10
But this requires me to run two queries. Is there a more efficient way to do this with a single query? (I'm using MySQL.)