按两个字段的总和排序

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

问题描述

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)