#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    13
    Rep Power
    0

    Php - mysql question


    I have a table that looks like this:

    -id
    -name
    -brand
    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
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    240
    Rep Power
    29
    You were so laconic that I am not sure I understand your task correct. However it sounds you want something like following:

    Code:
    select brand, count(name) as cnt from your_table group by cnt order by cnt desc;

    Comments on this post

    • badger_fruit agrees
    CodeAbbey - programming problems for novice coders
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    13
    Rep Power
    0
    Originally Posted by rodiongork
    You were so laconic that I am not sure I understand your task correct. However it sounds you want something like following:

    Code:
    select brand, count(name) as cnt from your_table group by cnt order by cnt desc;
    I would like to count how many brands I have of each brands for example

    Nike:15
    Adidas:3
    Puma:2

    And then print in that order:

    -Nike
    -Adidas
    -Puma

    I edited a little bit your query to:

    PHP Code:
    select brandcount(brand) as brand from spikes group by brand order by brand desc 
    But when I print $_row['brand'] It's printing the count value instead of the brand name.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    240
    Rep Power
    29
    I edited a little bit your query to:
    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.
    CodeAbbey - programming problems for novice coders
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    13
    Rep Power
    0
    You are right, I don't completely understand that query but I did it because I got the following error:
    Can't group on 'cnt'
    EDIT:
    I change the group by cnt to brand and it seems to be working.
    Thanks for your help!
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    240
    Rep Power
    29
    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 ;-)
    CodeAbbey - programming problems for novice coders

IMN logo majestic logo threadwatch logo seochat tools logo