php/mysql多重排序 [英] php/mysql multiple order by

查看:60
本文介绍了php/mysql多重排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在处理一个结果表,我想按点对表进行排序(最高点在顶部),如果点相等,我想按目标差排序,然后如果目标差等于我想按进球数排序.

I am working on a results table at the minute, I am wanting to sort a table, by points (highest at the top), if points are equal I want to sort by goal difference, and then if goal difference is equal I want to sort by goals scored.

所以表可能看起来像这样

So a table may look like this,

+--------+--------+----------+-----------------+--------+
|  Team  | Scored | Conceded | Goal Difference | Points |
+--------+--------+----------+-----------------+--------+
| Team A |     20 |       10 | +10             |     15 |
| Team B |     20 |       15 | +5              |     15 |
| Team C |     10 |       10 | 0               |      9 |
| Team D |      5 |       5  | 0               |      9 |
+--------+--------+----------+-----------------+--------+

A队之所以赢得联赛是因为它的进球差比B队好,C队的成绩高于D队,因为他们得分更高,并且其他所有条件都是平等的.

So Team A wins the league because it has a better goal difference than Team B, Team C finish above Team D because they score more goals, and all other things are equal.

是否可以在mysql中以这种方式订购,还是需要用PHP解析结果?

Is it possible to order this way in mysql, or will I need to parse the results with PHP?

推荐答案

猜怎么着,您可以将多个列名传递给ORDER BY

Guess what, you can pass multiple column names to ORDER BY

SELECT * FROM mytable ORDER BY Points DESC, `Goal Difference` DESC, Scored DESC 

您尚未提供表结构,但正如 jpg 所指出的那样,如果这些字段不是数字,则为ORDER按field_name + 0可能更合适

You haven't given your table structure, but as pointed out by jpg if these fields are not numeric, ORDER by field_name + 0 may be more appropriate

这篇关于php/mysql多重排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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