在单个MySQL查询中按两列的函数排序
让我们先创建一个表
mysql> create table orderByAFunctionDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> FirstNumber int, -> SecodNumber int -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into orderByAFunctionDemo(FirstNumber,SecodNumber) values(10,4); mysql> insert into orderByAFunctionDemo(FirstNumber,SecodNumber) values(45,78); mysql> insert into orderByAFunctionDemo(FirstNumber,SecodNumber) values(23,10); mysql> insert into orderByAFunctionDemo(FirstNumber,SecodNumber) values(67,237); mysql> insert into orderByAFunctionDemo(FirstNumber,SecodNumber) values(12,90);
使用select语句显示表中的所有记录。查询如下-
mysql> select *from orderByAFunctionDemo;
以下是输出
+----+-------------+-------------+ | Id | FirstNumber | SecondNumber| +----+-------------+-------------+ | 1 | 10 | 4 | | 2 | 45 | 78 | | 3 | 23 | 10 | | 4 | 67 | 237 | | 5 | 12 | 90 | +----+-------------+-------------+ 5 rows in set (0.00 sec)
这是按两列函数排序的查询。查询如下-
mysql> select (4*FirstNumber+5*SecondNumber) as Calculation -> from orderByAFunctionDemo -> order by Calculation desc;
以下是输出
+-------------+ | Calculation | +-------------+ | 1453 | | 570 | | 498 | | 142 | | 60 | +-------------+ 5 rows in set (0.00 sec)