降序

2014-12-30 18 views
0

通过选择不同的计数行我有一个表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

我选择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 
+0

现在如果我想结合到特定的时间意味着..........................'选择标记,计数(*)作为计数 从#标签 其中'time'> '2014年12月29日' 组由标签 为了通过计数递减 限制3'和'选择标签,COUNT(*)作为计数 从主题标签 其中'time'>“2013-12 -29'AND'time' <'2014-12-29' group by tag order by count desc limit 3'那怎么办? –

+0

我更新了答案。总结条件。但是你必须选择你想用来排序的列。 –

+0

第一个和第二个将有相等的行数? –

1
SELECT tag, count(*) as anz FROM hastags GROUP BY tag ORDER BY anz DESC 
+0

现在如果我想结合到特定的时间意味着..........................'select tag,count(*)如从主题标签 其中'time'计数 从主题标签 计数 其中'time'> '2014年12月29日' 组由标签 为了通过计数递减 限制3'和'选择标签,COUNT(*) >'2013-12-29'AND'time' <'2014-12-29' group by tag order by count desc limit 3'那怎么办? –

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;