MySQL 中两列之间的差异已排序?
mysqlmysqli database
为此使用 ORDER BY 子句。让我们首先创建一个表 −
mysql> create table DemoTable ( Value1 int, Value2 int ); Query OK, 0 rows affected (0.54 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values(10,40); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(50,5); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(51,56); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(52,78); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(90,7); Query OK, 1 row affected (0.21 sec)
使用 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)