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
这篇关于按两个字段的总和排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:按两个字段的总和排序


基础教程推荐
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 在多列上分布任意行 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01