在单个查询中对多列进行排序的 MySQL 查询
要对多列进行排序,请使用 ORDER BY GREATEST()。我们首先创建一个 -
mysql> create table DemoTable1395 -> ( -> Value1 int, -> Value2 int, -> Value3 int -> ); Query OK, 0 rows affected (0.79 sec)
使用 insert 在表中插入一些记录 -
mysql> insert into DemoTable1395 values(40,50,60); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1395 values(90,56,80); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1395 values(10,20,30); Query OK, 1 row affected (0.11 sec)
使用 select 从表中显示所有记录 -
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)
广告