MySQL查询在单个查询中将多个列排序在一起

要对多列进行排序,请使用ORDER BY GREATEST()。让我们首先创建一个-

mysql> create table DemoTable1395
   -> (
   -> Value1 int,
   -> Value2 int,
   -> Value3 int
   -> );

使用insert在表中插入一些记录-

mysql> insert into DemoTable1395 values(40,50,60);
mysql> insert into DemoTable1395 values(90,56,80);
mysql> insert into DemoTable1395 values(10,20,30);

使用选择显示表中的所有记录-

mysql> select * from DemoTable1395;

这将产生以下输出-

+--------+--------+--------+
| Value1 | Value2 | Value3 |
+--------+--------+--------+
|     40 |     50 |     60 |
|     90 |     56 |     80 |
|     10 |     20 |     30 |
+--------+--------+--------+
3 rows in set (0.00 sec)

这是对多列进行排序的查询-

mysql> select * from DemoTable1395
    -> order by greatest(Value1,Value2,Value3);

这将产生以下输出-

+--------+--------+--------+
| Value1 | Value2 | Value3 |
+--------+--------+--------+
|     10 |     20 |     30 |
|     40 |     50 |     60 |
|     90 |     56 |     80 |
+--------+--------+--------+
3 rows in set (0.00 sec)
猜你喜欢