mysql - Group query results by "today", "in last 7 days", "in last month" and "older" -
i have simple question related grouping rows date "narrative" periods. let's assume have simple table articles. id pk, title , date. date column datetime / timestamp.
i group somehow results can present them in view like
written today:
- art 944
- art 943
written in last 7 days:
- art 823
- art 743
written in last 30 days:
- art 520
- art 519
- art 502
older:
- art 4
- art 3
- art 1
can achieve in 1 single query group by statements?
gordon should have credit writing out. think want append column appropriate descriptor , sort them in order you'd see them.
select title, case when date = curdate() 'today' when date >= curdate() - interval 6 day 'last 7 days' when date >= curdate() - interval 29 day 'last 30 days' else 'older' end bucket, ... order case when date = curdate() 1 when date >= curdate() - interval 6 day 2 when date >= curdate() - interval 29 day 3 else 4 end, title ...
it looks didn't have titles in alphabetical order. if want them sorted age remove case expression , use date value.
Comments
Post a Comment