November 23rd, 2013, 01:26 PM
Php - mysql question
I have a table that looks like this:
and more few fields.
I would like to print a list of the most branded products.
I thought to make a query for each brand and count but it's a tone of code and ineffective.
So I hope you can help me with that
November 23rd, 2013, 01:34 PM
You were so laconic that I am not sure I understand your task correct. However it sounds you want something like following:
select brand, count(name) as cnt from your_table group by cnt order by cnt desc;
Comments on this post
November 23rd, 2013, 01:47 PM
I would like to count how many brands I have of each brands for example
Originally Posted by rodiongork
And then print in that order:
I edited a little bit your query to:
But when I print $_row['brand'] It's printing the count value instead of the brand name.
select brand, count(brand) as brand from spikes group by brand order by brand desc
November 23rd, 2013, 01:56 PM
It was quite bad idea to edit my query if you do not understand what you are doing.
Why you changed "as cnt" to "as brand"? Do not you see that now you have two columns named "brand" in the result? You should not change this and then you can use $_row['cnt'] for quantity and $_row['brand'] for brand name.
November 23rd, 2013, 01:59 PM
You are right, I don't completely understand that query but I did it because I got the following error:
I change the group by cnt to brand and it seems to be working.
Thanks for your help!
November 23rd, 2013, 02:23 PM
Ah yes, sorry, my fault. Of course it should be "group by brand order by cnt" - congratulations since it looks now you understand the matter ;-)