在 MySQL 中,按多列排序并未按预期执行?
以下为按多列排序的语法 −
select *from yourTableName order by yourColumnName1 DESC,yourColumnName2,yourColumnName3;
让我们创建一个表 −
mysql> create table demo29 −> ( −> value1 int, −> value2 int −> ); Query OK, 0 rows affected (1.67 sec)
在表中插入一些记录,使用 insert 命令 −
mysql> insert into demo29 values(10,500); Query OK, 1 row affected (0.15 sec) mysql> insert into demo29 values(14,400); Query OK, 1 row affected (0.14 sec) mysql> insert into demo29 values(9,500); Query OK, 1 row affected (0.12 sec) mysql> insert into demo29 values(13,400); Query OK, 1 row affected (0.11 sec) mysql> insert into demo29 values(45,500); Query OK, 1 row affected (0.14 sec) mysql> insert into demo29 values(11,400); Query OK, 1 row affected (0.10 sec)
使用 select 语句从表中显示记录 −
mysql> select *from demo29;
这将产生以下输出 −
+--------+--------+ | value1 | value2 | +--------+--------+ | 10 | 500 | | 14 | 400 | | 9 | 500 | | 13 | 400 | | 45 | 500 | | 11 | 400 | +--------+--------+ 6 rows in set (0.00 sec)
以下为按多列排序的查询 −
mysql> select *from demo29 −> order by value2 desc,value1;
这将产生以下输出 −
+--------+--------+ | value1 | value2 | +--------+--------+ | 9 | 500 | | 10 | 500 | | 45 | 500 | | 11 | 400 | | 13 | 400 | | 14 | 400 | +--------+--------+ 6 rows in set (0.00 sec)
广告