Can any help me with such type of query.
I have:
posts
tablecomments
table
They are linked through comments.post_id = posts.post_id
columns.
I user can filter post by comments for the past:
- 1 hour
- 24 hours
- 2 days, etc.
If user selected to show posts for the past 1 hour, but there no posts this period, we need to go step by step:
Select posts for past 1 hour
, if empty - for past 24 hours
, if empty - for past 2 days
, if empty - since inception (without any conditions).
Could anyone please help me to build such query?
UPD
"Filter posts by comments" means sort by comments count.
So actually goal is request "Show me posts sorted by comments count that have been left for the past XXX hours".
And if is selected "for the past hour" but there are no posts with comments left for the past 1 hour, we need to fetch posts with comments left for the past 24 hours (sorted by comments count) and so on.
Tables structure
Posts:
- post_id
- title
- content
- date_added
Comments
- comment_id
- content
- post_id
- date_added
So link is posts.post_id = comments.post_id
.
I would like to have next result when user view most commented posts for the past hour:
posts.post_id | comments_count | posts.date_added | group
---------------+----------------+------------------+----------------
156 | 8 | 2013-04-02 | hour
154 | 3 | 2013-04-02 | hour
129 | 1 | 2013-03-10 | 24 hours
13 | 14 | 2013-02-18 | 48 hours
138 | 6 | 2013-03-29 | week
137 | 4 | 2013-03-29 | week
161 | 21 | 2013-04-11 | month
6 | 2 | 2013-01-24 | year
103 | 8 | 2013-03-02 | since inception
Results sorted by:
- Top of the list is 2 posts that have been commented due the past hour, and ordered by comments count.
- Next we place posts that have been commented due the past day.
- Next — posts commented due past 2 days
- posts commented due past week, and again they should be ordered by comments count
- For the past month
- For the past year
- In the end of this list we need to place articles that have been commented more than year ago, and they also should be ordered by comments count.
Thanks in advance.