Forum Moderators: coopster

Message Too Old, No Replies

ordering categories and fields

         

PokeTech

8:53 pm on Mar 22, 2009 (gmt 0)

10+ Year Member



This is going to be hard to explain but I have two tables. One is the categories that has id,name,date and the other one is images id,name,image,date,category

I want to be able to order the categories by how many images there are in each category but I don't want to make another field in the categories table that has to be edited every time someone adds an image to it.

For example if this were the different images (their categories): 1,1,2,5,4,3,5,3,6,1,1,2

It would order them like this: 1,2,3,4,5,6 but then it would also say how many of each there are next to it: 1:4,2:2,5:2,3:1,4:1,6:1

Is there anyway to do this? If someone gets this thank you!

[edited by: PokeTech at 8:56 pm (utc) on Mar. 22, 2009]

d40sithui

3:54 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



this query should do the trick:

"SELECT category, count(category) as number FROM images GROUP BY category ORDER BY number DESC";