tags:

views:

22

answers:

3

i wanted to retrieve rows from the mysql database and order them by votes:

votes_up + votes_down = votes

table:

posts{id, post_text, votes_up, votes_down, date}
ORDER BY votes
+2  A: 
SELECT id, post_text, votes_up, votes_down, date, sum(votes_up + votes_down) as 'Votes'
FROM posts
ORDER BY sum(votes_up + votes_down)
Bryan Denny
thanks for the great answer ;), can i aslo ask you to access the votes_up or votes_down values, deos that automatically come this query
getaway
Add the other columns you need to the select statement, I'll update my answer
Bryan Denny
+1  A: 

Traditional SQL allows you to use column aliases in the ORDER BY:

  SELECT p.votes_up + p.votes_down AS total_votes
    FROM POSTS p
ORDER BY total_votes
OMG Ponies
did you mean p.votes_down aswell? :))
getaway
@getaway: Sorry?
OMG Ponies
+1  A: 

You can use blablabla ORDER BY sum(votes_up + votes_down), but be careful and don't use this on high-loaded production databases, because sum() will be calculeted "on the fly" and it will be very slow for large tables!

seriyPS
im just doing it on the posts made in the last 3 hours, so it wont perform on all the rows in the post tbale, would that still be alright
getaway
is thier alternative to doing this, the most important thing is that i order the rows on total votes
getaway