我们可以通过数学运算订购MySQL结果吗?
是的,我们可以使用ORDERBY子句对数学运算进行排序。让我们首先创建一个表:
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)
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短