如何按MySQL中两列的最大值排序?
让我们首先创建一个表-
mysql> create table DemoTable834( Value1 int, Value2 int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable834 values(10,20); mysql> insert into DemoTable834 values(40,50); mysql> insert into DemoTable834 values(20,24); mysql> insert into DemoTable834 values(30,10);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable834;
这将产生以下输出-
+--------+--------+ | Value1 | Value2 | +--------+--------+ | 10 | 20 | | 40 | 50 | | 20 | 24 | | 30 | 10 | +--------+--------+ 4 rows in set (0.00 sec)
以下是使用以下命令按两列中的最大值排序的查询GREATEST()
:
mysql> select *from DemoTable834 order by greatest(Value1,Value2) DESC;
这将产生以下输出-
+--------+--------+ | Value1 | Value2 | +--------+--------+ | 40 | 50 | | 30 | 10 | | 20 | 24 | | 10 | 20 | +--------+--------+ 4 rows in set (0.00 sec)