按两个字段的总和排序 [英] Order By Sum of Two Fields

查看:31
本文介绍了按两个字段的总和排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一张包含 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

这篇关于按两个字段的总和排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆