MySQL查询显示按数字顺序排序的记录?

使用ORDER BY并设置差异以显示按数字差异排序的记录。以下是语法-

select *from yourTableName
order by (yourIntegerColumnName1 - yourIntegerColumnName2);

让我们首先创建一个表-

mysql> create table DemoTable1313
-> (
-> Name varchar(20),
-> Score1 int,
-> Score2 int
-> );

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable1313 values('Chris',40,60);
mysql> insert into DemoTable1313 values('David',70,50);
mysql> insert into DemoTable1313 values('Adam',35,30);

使用select语句显示表中的所有记录-

mysql> select *from DemoTable1313;

输出结果

+-------+--------+--------+
| Name  | Score1 | Score2 |
+-------+--------+--------+
| Chris |     40 |     60 |
| David |     70 |     50 |
| Adam  |     35 |     30 |
+-------+--------+--------+
3 rows in set (0.00 sec)

以下是按数字差异排序的查询-

mysql> select *from DemoTable1313
-> order by (Score1-Score2);

输出结果

+-------+--------+--------+
| Name | Score1  | Score2 |
+-------+--------+--------+
| Chris | 40     |    60  |
| Adam  | 35     |    30  |
| David | 70     |    50  |
+-------+--------+--------+
3 rows in set (0.00 sec)