0

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.

  • possibly duplicate http://stackoverflow.com/questions/12789396/how-to-get-multiple-counts-with-one-sql-query – Muhammad Dec 10 '13 at 09:11

3 Answers3

1

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
0

try this :

select name, count(name) as total from animals
 group by name order by total desc limit 10
uvais
  • 416
  • 2
  • 6
0

Try:

select 
    top 10 type, Count(*) 
from 
    animals 
group by 
    type 
order by 
    count(*) desc,type
TheHippo
  • 61,720
  • 15
  • 75
  • 100
Krishna
  • 170
  • 7