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.

1

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

0

try this :

select name, count(name) as total from animals
 group by name order by total desc limit 10

Comments

0

Try:

select 
    top 10 type, Count(*) 
from 
    animals 
group by 
    type 
order by 
    count(*) desc,type

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.