我有一张表hastags
id|tweet_id| tag |time
1 1 hell
2 2 hellyeah
3 3 hell
4 4 stackoverflow
5 5 hell
6 6 hellyeah
7 7 bellrings
8 7 nomorehell
8 7 nomorehell
我想在特定时间之上选择最常用的 tags
的数量。
现在,如果我将查询限制为3行,我应该
|tag | count|
hell 3
hellyeah 2
nomorehell 2
如何帮助我实现这一目标?
答案 0 :(得分:1)
SELECT tag, count(*) as anz FROM hastags GROUP BY tag ORDER BY anz DESC
答案 1 :(得分:0)
我选择2014-12-29
作为特定时间的示例
select tag,
sum(`time` > '2014-12-29') as sum_greater,
sum(`time` < '2014-12-29') as sum_smaller
from hashtags
group by tag
order by sum_greater desc, sum_smaller desc
limit 3
答案 2 :(得分:0)
考虑到开始时间和结束时间,则此查询将起作用。
SELECT tag, count(tag) FROM hastags where (start <= end and :time >= start and :time <= end) or
(end < start and (:time <= end or :time >= start)) GROUP BY tag;