为此使用ORDER BY子句。让我们首先创建一个表-
mysql> create table DemoTable ( Value1 int, Value2 int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(10,40); mysql> insert into DemoTable values(50,5); mysql> insert into DemoTable values(51,56); mysql> insert into DemoTable values(52,78); mysql> insert into DemoTable values(90,7);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+--------+--------+ | Value1 | Value2 | +--------+--------+ | 10 | 40 | | 50 | 5 | | 51 | 56 | | 52 | 78 | | 90 | 7 | +--------+--------+ 5 rows in set (0.00 sec)
以下是获取两列之间的排序差异的查询-
mysql> select (Value1-Value2) as Sub from DemoTable order by Sub DESC;
输出结果
+------+ | Sub | +------+ | 83 | | 45 | | -5 | | -26 | | -30 | +------+ 5 rows in set (0.00 sec)