簡體   English   中英

查找最頻繁的值和排序

[英]Find the most frequent value and order by it

+-----------------------+------------------------+
| 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

請不要將此標記為重復項。

請嘗試以下方法:

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

嘗試這個:-

select being_followed,count(*) total_followers
from table
group by being_followed
order by total_followers desc
SELECT being_followed , count(being_followed )as counter FROM `table_Name` GROUP BY being_followed ORDER BY counter DESC

您將得到想要的結果。在此處使用group by將獲得唯一值,在使用count時將獲得相同的being_followed計數器

嘗試這個:

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 |
+-----------------------+------------------------+

嘗試這個

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

獲取being_followed的計數,並按從高到低的順序排序(降序)

也許您可以嘗試以下方法:

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

一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM