是的,我们可以使用ORDER BY子句对数学运算进行排序。让我们首先创建一个表:
mysql> create table orderByMathCalculation -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Quantity int, -> Price int -> );
以下是使用insert命令在表中插入一些记录的查询:
mysql> insert into orderByMathCalculation(Quantity,Price) values(10,50); mysql> insert into orderByMathCalculation(Quantity,Price) values(20,40); mysql> insert into orderByMathCalculation(Quantity,Price) values(2,20); mysql> insert into orderByMathCalculation(Quantity,Price) values(11,10);
以下是使用select语句显示表中所有记录的查询:
mysql> select *from orderByMathCalculation;
这将产生以下输出
+----+----------+-------+ | Id | Quantity | Price | +----+----------+-------+ | 1 | 10 | 50 | | 2 | 20 | 40 | | 3 | 2 | 20 | | 4 | 11 | 10 | +----+----------+-------+ 4 rows in set (0.00 sec)
情况1:这是按数学运算以升序进行排序的查询。
mysql> select *from orderByMathCalculation order by Quantity*Price;
这将产生以下输出
+----+----------+-------+ | Id | Quantity | Price | +----+----------+-------+ | 3 | 2 | 20 | | 4 | 11 | 10 | | 1 | 10 | 50 | | 2 | 20 | 40 | +----+----------+-------+ 4 rows in set (0.00 sec)
情况1:这是按数学顺序以降序进行查询的查询。
mysql> select *from orderByMathCalculation order by Quantity*Price desc;
这将产生以下输出
+----+----------+-------+ | Id | Quantity | Price | +----+----------+-------+ | 2 | 20 | 40 | | 1 | 10 | 50 | | 4 | 11 | 10 | | 3 | 2 | 20 | +----+----------+-------+ 4 rows in set (0.00 sec)