Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calculate votes and ORDER BY DESC

    Hi, this is my first post. I have 2 fields, Name and Vote. I would like to get 5 names with the most vote and put it in DESC order. The most vote is placed top and followed by the second most.

    Code:
     SELECT Name, SUM( vote ) 
    FROM table_vote
    GROUP BY Name
    DESC
    LIMIT 5
    This is what I have and it's not correct. Any help is appreciated. Thanks!

  • #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    you have exactly what you need. sort of!

    give the sum(vote) an alias, it is easier to refer to it than typing sum(vote).

    secondly you don't have an ORDER BY clause, you specifically need that when you want to order your results and then you use ASC or DESC (actually it defaults to ASC so you don't specifically need that).

    Code:
     SELECT Name, SUM( vote ) as votetotal
    FROM table_vote
    GROUP BY Name
    ORDER BY
    votetotal DESC
    LIMIT 5
    welcome to
    CodingForums by the way.

  • #3
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks, that did the trick. appreciate it


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •