如何按两列排序,如果结果相等,请考虑另一列? [英] How can I order by two columns, if result is equal, consider another column?

查看:52
本文介绍了如何按两列排序,如果结果相等,请考虑另一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个叫做marks、total_marks 和aggregate 的表.我想首先根据聚合对结果进行排序,但是当同一列中的聚合等于下一个时,则考虑总数最高的学生.即按聚合排序,但如果最后一个聚合等于新聚合,那么我如何将最高排名给予总和更高的一个?

I have tables called marks, total_marks, and aggregate. I want to sort the results first depending on the aggregate, but when the aggregate in the same column is equal to the next, then consider the student with the highest total. I.e., order by aggregate, but if the last aggregate is equal to the new one, how can I then give the highest rank to the one with higher total?

推荐答案

使用 SQL 您可以按多列排序:

With SQL you can sort by multiple columns:

SELECT * from marks ORDER BY aggregate DESC, total_marks DESC

这将首先按聚合(最高在前)对所有行进行排序,然后具有相同聚合值的行将按total_marks"(也是最高的在前)进行排序.您可以通过将排序方向从 DESC(结尾)(即最高优先)更改为 ASC(结束)(即最低优先)来反转单个列的排序

This will sort all your rows by aggregate first (highest first) and rows with the same aggregate value will then be sorted by "total_marks" (also highest first). You can invert the sorting for the individual columns by changing the sort direction from DESC(ending) (i.e. highest first) to ASC(ending) (i.e. lowest first)

这篇关于如何按两列排序,如果结果相等,请考虑另一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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