按两个字段的总和排序

时间:2022-06-02 11:48:22

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.

假设我有一个包含karma_up和karma_down的表。每当有人投票时,karma_up会增加,每次有人投票时,karma_down也会增加。我如何提取这些选择的行并按这些新值的总和对它们进行排序? ORDER BY(karma_up - karma_down)似乎没有按照我想要的方式工作。我只想把具有最高业力的行放在最前面。

3 个解决方案

#1


19  

Very simple

很简单

SELECT 
ID, KARMA_UP, KARMA_DOWN, (KARMA_UP-KARMA_DOWN) AS USER_KARMA 
FROM KARMA 
ORDER BY USER_KARMA DESC

#2


6  

SELECT *, karma_up - karma_down AS karma_total 
FROM MyTable
ORDER BY karma_total DESC;

#3


0  

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

#1


19  

Very simple

很简单

SELECT 
ID, KARMA_UP, KARMA_DOWN, (KARMA_UP-KARMA_DOWN) AS USER_KARMA 
FROM KARMA 
ORDER BY USER_KARMA DESC

#2


6  

SELECT *, karma_up - karma_down AS karma_total 
FROM MyTable
ORDER BY karma_total DESC;

#3


0  

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