降序

2014-12-30 20 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;