MySQL 从不同的起始值排序?
首先,让我们创建一个表 -
mysql> create table DemoTable ( ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientName varchar(30) ); Query OK, 0 rows affected (0.74 sec)
使用 insert 命令在表中插入记录 -
mysql> insert into DemoTable(ClientName) values('John'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(ClientName) values('Chris'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(ClientName) values('Robert'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(ClientName) values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(ClientName) values('Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(ClientName) values('Carol'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(ClientName) values('Sam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(ClientName) values('Mike'); Query OK, 1 row affected (0.12 sec)
使用 select 语句从表中显示所有记录 -
mysql> select * from DemoTable;
这将生成以下输出 -
+----------+------------+ | ClientId | ClientName | +----------+------------+ | 1 | John | | 2 | Chris | | 3 | Robert | | 4 | David | | 5 | Bob | | 6 | Carol | | 7 | Sam | | 8 | Mike | +----------+------------+ 8 rows in set (0.00 sec)
以下是 MySQL 从某个起始值进行排序的查询 -
mysql> select *from DemoTable order by ClientId < 5,ClientId;
这将生成以下输出 -
+----------+------------+ | ClientId | ClientName | +----------+------------+ | 5 | Bob | | 6 | Carol | | 7 | Sam | | 8 | Mike | | 1 | John | | 2 | Chris | | 3 | Robert | | 4 | David | +----------+------------+ 8 rows in set (0.00 sec)
广告