Reputation: 37
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.
Upvotes: 0
Views: 180
Reputation: 170
Try:
select
top 10 type, Count(*)
from
animals
group by
type
order by
count(*) desc,type
Upvotes: 0
Reputation: 416
try this :
select name, count(name) as total from animals
group by name order by total desc limit 10
Upvotes: 0
Reputation: 870
Please Try the following query:
Select type, count(type) as type_count FROM animals GROUP BY type ORDER BY type_count desc LIMIT 0, 10
Upvotes: 1