MySQL 按特定列 x 排序并在小数点后升序显示剩余值
让我们首先创建一个表 −
mysql> create table DemoTable -> ( -> MonthNumber int -> ); Query OK, 0 rows affected (1.68 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(9); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(6); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(8); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(5); Query OK, 1 row affected (0.11 sec)
使用 select 语句从表中显示所有记录 −
mysql> select * from DemoTable;
这将生成以下输出 −
+-------------+ | MonthNumber | +-------------+ | 10 | | 1 | | 9 | | 6 | | 8 | | 5 | +-------------+ 6 rows in set (0.00 sec)
以下是对特定列进行排序并从小数点后升序显示剩余记录的查询 −
mysql> select * from DemoTable -> order by MonthNumber=5 desc,MonthNumber asc;
这将生成以下输出 −
+-------------+ | MonthNumber | +-------------+ | 5 | | 1 | | 6 | | 8 | | 9 | | 10 | +-------------+ 6 rows in set (0.00 sec)
广告