简体   繁体   English

在SQL中选择前5个最多计数国家/地区

[英]Selecting top 5 most count countries in SQL

Hello I need to take most countable countries from DB. 您好我需要从DB中获取大多数可数国家/地区。 how it looks like: 看起来如何:

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

And I want output from this - from the most count like: 我想要输出 - 从最重要的计数如下:

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

I was tried with something like this but not working 我曾尝试过类似的东西但没有工作

$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);

The correct syntax in MySQL uses LIMIT , not TOP : MySQL中的正确语法使用LIMIT ,而不是TOP

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

Use must use LIMIT to get top 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