MySQL 如何更改项目的顺序
要在MySQL中更改项目的顺序,请使用 ORDER BY 别名。让我们首先创建一个表 –
mysql> create table DemoTable653 (Product1Amount int,Product2Amount int);
Query OK, 0 rows affected (0.42 sec)
使用插入命令在表中插入一些记录 –
mysql> insert into DemoTable653 values(400,250);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable653 values(500,300);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable653 values(40,400);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable653 values(200,450);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable653 values(50,20);
Query OK, 1 row affected (0.10 sec)
使用 select 语句显示表中的所有记录 –
mysql> select *from DemoTable653;
这将产生以下输出 –
+----------------+----------------+
| Product1Amount | Product2Amount |
+----------------+----------------+
| 400 | 250 |
| 500 | 300 |
| 40 | 400 |
| 200 | 450 |
| 50 | 20 |
+----------------+----------------+
5 行记录 (0.00 秒)
以下是更改项目顺序并显示差异的查询 –
mysql> select Product1Amount,Product2Amount,(Product1Amount-Product2Amount) as ProductAmountDifference from DemoTable653 order by ProductAmountDifference;
这将产生以下输出 –
+----------------+----------------+-------------------------+
| Product1Amount | Product2Amount | ProductAmountDifference |
+----------------+----------------+-------------------------+
| 40 | 400 | -360 |
| 200 | 450 | -250 |
| 50 | 20 | 30 |
| 400 | 250 | 150 |
| 500 | 300 | 200 |
+----------------+----------------+-------------------------+
5 行记录 (0.29 秒)
阅读更多:MySQL 教程