[英]SQL Count rows and display in order
假设我有一个名为animals的表,其中的列名为“ id”,“ name”和“ type”。 最后一栏是牛,鸡,马,猪,大象,河马等。
我想要的是一个查询类型的数量并按顺序显示它们的查询...
鸡45
牛40
马5
等等...
现在我只想显示最高金额的10。 我用这个查询...
$result = mysql_query("SELECT * FROM animals ORDER BY id DESC LIMIT 0, 10");
while($row = mysql_fetch_array($result))
{
echo "<tr><td>" . $row['type']. "</td><td align=\"right\"></td></tr>";
}
上面的代码仅显示如下类型
马
鸡
鸡
牛
鸡
牛
牛
马
等等...
我不知道如何使用计数器并按最高价值排序。
请尝试以下查询:
Select type, count(type) as type_count FROM animals GROUP BY type ORDER BY type_count desc LIMIT 0, 10
尝试这个 :
select name, count(name) as total from animals
group by name order by total desc limit 10
尝试:
select
top 10 type, Count(*)
from
animals
group by
type
order by
count(*) desc,type
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.