[英]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.