Crooked Posted April 12, 2011 Share Posted April 12, 2011 Hey guys, I'm currently trying to count how many of the same kind of "id" there is in a certain table and then it should print out the rows by either ASC or DESC starting or ending on the highest/lowest. To understand it better: I have a table where i have 2 different types of rows ( cake / cofee ) In the "cake" row it could possibly be something like - Cake - row 1 1 1 1 1 2 2 i want help with how i can do a COUNT on how many number 1 there is and how many number 2 there is and etc to about 5, and then have em print out the cakes name from the highest bought one to the lowest one. Thanks Link to comment https://forums.phpfreaks.com/topic/233509-simple-count-help/ Share on other sites More sharing options...
gizmola Posted April 12, 2011 Share Posted April 12, 2011 To get counts per group you need to do a GROUP BY on the column that has the type ID's. I have no idea what your table structure looks like, but something like this: SELECT COUNT(*) as countof, rowtype FROM cake GROUP BY rowtype ORDER BY countof DESC LIMIT 5 Link to comment https://forums.phpfreaks.com/topic/233509-simple-count-help/#findComment-1200691 Share on other sites More sharing options...
gizmola Posted April 12, 2011 Share Posted April 12, 2011 FWIW, if you only have 2 types (cake vs coffee) you will only have 2 groups. You'll get a group per DISTINCT value in the column you're grouping on. Link to comment https://forums.phpfreaks.com/topic/233509-simple-count-help/#findComment-1200692 Share on other sites More sharing options...
Jeffro Posted April 12, 2011 Share Posted April 12, 2011 Looks to me like you're trying to do the same thing I was a couple months. Check this thread and see if it provides the solution for you as it did for me. Link to comment https://forums.phpfreaks.com/topic/233509-simple-count-help/#findComment-1200701 Share on other sites More sharing options...
Crooked Posted April 12, 2011 Author Share Posted April 12, 2011 here is a image of what i was trying to get to: and forget about the cofee table. So simple put i wanted to make a hit-list or top-list of a sort because i havn't gotten around to get familier with the count that much. (FYI this is a example i quickly threw together, i left the original files on my laptop which is at home) Link to comment https://forums.phpfreaks.com/topic/233509-simple-count-help/#findComment-1200704 Share on other sites More sharing options...
Crooked Posted April 12, 2011 Author Share Posted April 12, 2011 Ignore my last post - This problem has been solved Thank you guys very very much Link to comment https://forums.phpfreaks.com/topic/233509-simple-count-help/#findComment-1200706 Share on other sites More sharing options...
fenway Posted April 19, 2011 Share Posted April 19, 2011 Then post the solution. Link to comment https://forums.phpfreaks.com/topic/233509-simple-count-help/#findComment-1203433 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.