按两个字段的总和排序

2021-12-21 00:00:00 sql mysql cakephp

假设我有一张包含 karma_up 和 karma_down 的表.每次有人投票支持 karma_up 都会增加,每次有人投票反对 karma_down 也会增加 1.我怎样才能拉出这些行的选择并让它们按这些新值的总和排序?ORDER BY (karma_up - karma_down) 似乎并不像我想要的那样工作.我只想将业力最高的行放在最上面.

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.

推荐答案

很简单

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

相关文章