要从排序列表的中间选择结果,请使用ORDER BY子句以及LIMIT。
让我们首先创建一个表。以下是查询-
mysql> create table sortedListDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(100) -> );
以下是使用insert命令在表中插入一些记录的查询-
mysql> insert into sortedListDemo(StudentName) values('John'); mysql> insert into sortedListDemo(StudentName) values('Sam'); mysql> insert into sortedListDemo(StudentName) values('Adam'); mysql> insert into sortedListDemo(StudentName) values('James'); mysql> insert into sortedListDemo(StudentName) values('Jace'); mysql> insert into sortedListDemo(StudentName) values('Mike'); mysql> insert into sortedListDemo(StudentName) values('Carol'); mysql> insert into sortedListDemo(StudentName) values('Bob'); mysql> insert into sortedListDemo(StudentName) values('Ramit'); mysql> insert into sortedListDemo(StudentName) values('Chris'); mysql> insert into sortedListDemo(StudentName) values('Robert'); mysql> insert into sortedListDemo(StudentName) values('David');
以下是使用select语句显示表中所有记录的查询-
mysql> select * from sortedListDemo;
这将产生以下输出-
+----+-------------+ | Id | StudentName | +----+-------------+ | 1 | John | | 2 | Sam | | 3 | Adam | | 4 | James | | 5 | Jace | | 6 | Mike | | 7 | Carol | | 8 | Bob | | 9 | Ramit | | 10 | Chris | | 11 | Robert | | 12 | David | +----+-------------+ 12 rows in set (0.00 sec)
以下是从排序列表中间选择结果的查询。我们将LIMIT设置为4,6,这意味着将随机显示6条记录-
mysql> select *from sortedListDemo -> order by StudentName -> LIMIT 4,6;
这将产生以下输出-
+----+-------------+ | Id | StudentName | +----+-------------+ | 12 | David | | 5 | Jace | | 4 | James | | 1 | John | | 6 | Mike | | 9 | Ramit | +----+-------------+ 6 rows in set (0.00 sec)