Let's say I have a table with karma_up and karma_down. Everytime someone votes up karma_up gets incremented and everytime someone votes down karma_down gets incremented one as well. How can I pull these a selection of rows and have them ordered by the sum of these new values? ORDER BY (karma_up - karma_down) does not seem to work how I want. I simply want the rows with the highest karma up top.
A:
not quite what you want but:
Order By karma_up ASC Order By karma_down DESC
Justin
2009-08-20 22:40:02
+4
A:
Very simple
SELECT
ID, KARMA_UP, KARMA_DOWN, (KARMA_UP-KARMA_DOWN) AS USER_KARMA
FROM KARMA
ORDER BY USER_KARMA DESC
Virat Kadaru
2009-08-20 22:40:18
The OP wants the highest on top and I think he needs DESC as Bill Karwin posted.
Leonel Martins
2009-08-20 22:45:00
Thanks. I fixed my answer
Virat Kadaru
2009-08-20 22:49:52
That's exactly what I wanted! To note, make sure they both columns are signed else your gonna get some fucked up results.
2009-08-20 23:09:44
A:
Does this work? If not, could you include the results in your question? Ordering on an expression ought to work as expected.
SELECT `post_id`, `karma_up`, `karma_down`, `karma_up` - `karma_down` AS `total`
ORDER BY `total` DESC
John Millikin
2009-08-20 22:40:51
+2
A:
SELECT *, karma_up - karma_down AS karma_total
FROM MyTable
ORDER BY karma_total DESC;
Bill Karwin
2009-08-20 22:41:01