假设我有一个名为动物的表,其列名为"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>";
}上面的代码只显示了以下类型
马
鸡肉
鸡肉
奶牛
鸡肉
奶牛
奶牛
马
等等。
我不知道如何使用计数器并按最高价值排序。
发布于 2013-12-10 09:10:46
请尝试以下查询:
Select type, count(type) as type_count FROM animals GROUP BY type ORDER BY type_count desc LIMIT 0, 10发布于 2013-12-10 09:12:34
试试这个:
select name, count(name) as total from animals
group by name order by total desc limit 10发布于 2013-12-10 09:14:55
尝试:
select
top 10 type, Count(*)
from
animals
group by
type
order by
count(*) desc,typehttps://stackoverflow.com/questions/20490153
复制相似问题