mysql count issue

Discussion in 'PHP' started by Fracisc, Nov 24, 2011.

  1. #1
    Hi there!

    I have a table where I have banners added with ID. When someone uses a banner it is added to the database again.

    So, it is like

    userx bannery
    userz bannery

    etc.

    I can do count(bannery) to see how many times the banner with ID bannery is in my DB. WHat I need now is to count all the banners by ID and display the top 10 most used banners. Also, this needs to consume as few mysql recourses as possible.

    I am not sure how can I do that. Any kind of help is much appreciated.

    Thank you!
     
    Fracisc, Nov 24, 2011 IP
  2. 2WDH.com

    2WDH.com Active Member

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    5
    Trophy Points:
    68
    #2
    Hi Fracisc.

    Try this:
    SELECT banner_id_field, COUNT(*) FROM banners_table GROUP BY banner_id_field ORDER BY COUNT(*) DESC LIMIT 10
    Code (markup):
     
    2WDH.com, Nov 24, 2011 IP
  3. luckyguy354

    luckyguy354 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sorry, just edit your code. Usually use a field in mysql function will give less time.
    SELECT banner_id_field, COUNT(banner_id_field) AS counter
    FROM banners_table
    GROUP BY banner_id_field
    ORDER BY COUNT(banner_id_field) DESC
    LIMIT 10
    Code (markup):
     
    luckyguy354, Dec 16, 2011 IP