Let's say i have a table named animals with with columns named "id" and "name" and "type". The last column is filled with cow, chicken, horse, pig, elephant, hippo etc.
What i want is a query that counts the number of types and displays them in order like this...
chicken 45
cows 40
horse 5
etc...
Now i only want to show the 10 with the highest amount. I use this query...
$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>";
}
The code above shows only the types like
horse
chicken
chicken
cow
chicken
cow
cow
horse
etc...
I don't know how to use the counter and sort in highest value.
请尝试以下查询:
Select type, count(type) as type_count FROM animals GROUP BY type ORDER BY type_count desc LIMIT 0, 10
try this :
select name, count(name) as total from animals
group by name order by total desc limit 10
Try:
select
top 10 type, Count(*)
from
animals
group by
type
order by
count(*) desc,type
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.