2016-01-06 47 views
0
+-----------------------+------------------------+ 
| being_followed  | follower    | 
+-----------------------+------------------------+ 
| Bob Dylan    |      B | 
| Bob Dylan    |      A | 
| Sam Cooke    |      X | 
| The Beatles   |      Y | 
| Bob Dylan    |      M | 
| Sam Cooke    |      N | 
+-----------------------+------------------------+ 

現在,我想找到哪個是being_followed最經常發生的價值,然後命令它。 它看起來有點像 -找到最常見的價值和它的順序

Bob Dylan - 3 
Sam Cooke - 2 
The Beatles - 1 

請不要將此標記爲重複。

+0

top 2000? (來自荷蘭的名單) –

+1

[按每個值COUNT排序]的可能重複(http://stackoverflow.com/questions/2283305/order-by-count-per-value) –

回答

0

試試這個: -

select being_followed,count(*) total_followers 
from table 
group by being_followed 
order by total_followers desc 
+0

'追隨者不應該在'羣組中by'。它不會給出正確的結果。請驗證。 – minatverma

+0

謝謝...更新 –

+0

另外,'order by'應該根據每個問題的'count'。 – minatverma

1

嘗試以下:

select being_followed , count(1) as count 
from table 
group by being_followed 
order by count desc ; 
+0

謝謝。這對我來說很好! :) –

+0

@VatsalSharma如果它回答你的問題,請標記爲已接受。 – minatverma

+0

我想知道如果答案是正確的,那麼爲什麼會投票。 – minatverma

0
SELECT being_followed , count(being_followed)as counter FROM `table_Name` GROUP BY being_followed ORDER BY counter DESC 

你會得到結果,您使用組由您將獲得獨特的價值和使用count你會want.Here什麼得到相同的計數器being_followed

0

試試這個:

SELECT being_followed,COUNT(*) AS follower 
FROM tablename GROUP BY being_followed ORDER BY follower DESC; 

輸出:

+-----------------------+------------------------+ 
| being_followed  | follower    | 
+-----------------------+------------------------+ 
| Bob Dylan    |      3 | 
| Sam Cooke    |      2 | 
| The Beatles   |      1 | 
+-----------------------+------------------------+ 
0

嘗試的being_followed這

SELECT being_followed,COUNT(1) count_followers 
FROM table 
GROUP BY being_followed 
ORDER BY COUNT(1) DESC; 

獲取計數,也爲了通過從高到低基地(降序)

0

也許你可以試試這個:

select being_followed, count(*) follower 
from TableName 
group by being_followed 
order by follower desc 

它工作正常。