首页 > 解决方案 > MySQL 条件排序

问题描述

假设我的users表中有以下数据。

name  | score
John  | 2
Dave  | 8
Joe   | -3
Sally | -20
Rose  | 0

如何按分数列对以下格式的用户进行排序?

John, Dave, Rose, Sally and Joe

换句话说,那些得分为正的用户应该首先升序,然后是 0,然后是负分的升序。

标签: mysqlsqlsql-order-by

解决方案


考虑:

select * from mytable order by (score < 0), (score = 0), score

DB Fiddle 上的演示


推荐阅读