...

View Full Version : Calculate votes and ORDER BY DESC



st_rage
07-21-2007, 12:40 PM
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.


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!

guelphdad
07-21-2007, 02:24 PM
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).



SELECT Name, SUM( vote ) as votetotal
FROM table_vote
GROUP BY Name
ORDER BY
votetotal DESC
LIMIT 5


welcome to
CodingForums by the way.

st_rage
07-21-2007, 02:34 PM
thanks, that did the trick. appreciate it



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum