簡體   English   中英

在SQL中選擇前5個最多計數國家/地區

[英]Selecting top 5 most count countries in SQL

您好我需要從DB中獲取大多數可數國家/地區。 看起來如何:

id name country
1  fsdf Sweden
2  dfdf Brazil
3  fgfg Sweden
4  gfgg Germany
5  fgfg Germany
6  fgfg Poland
7  fdff Germany
8  iuii Brazil
9  tyyt Sweden
10 tyut Sweden
11 gfgf Germany
12 fdff Holland

我想要輸出 - 從最重要的計數如下:

1 Germany count 4
2 Sweden count 4
3 Brazil count 2
4 Poland count 1
5 Holand coun 1

我曾嘗試過類似的東西但沒有工作

$top5 = $mysqli -> query ("SELECT top 5 country, count(*)
from list_ots
group by country
order by country desc");

while ($row = mysql_fetch_assoc($top5)) {
    echo $row["country"];
}
mysql_free_result($result);

MySQL中的正確語法使用LIMIT ,而不是TOP

select country, count(*) as cnt
from list_ots
group by country
order by cnt desc
limit 5;

使用必須使用LIMIT獲得前5名:

SELECT country, count(*) as count
FROM list_ots
GROUP BY country
ORDER by count desc
LIMIT 5;

select top 5 Country,Count(*)as CountryCount FROM list_ots Group By Country Order By CountryCount Desc

暫無
暫無

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

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