如何使用 MySQL LIMIT 对记录进行排序和提取?
我们先创建一个表 −
mysql> create table DemoTable679(FirstName varchar(100)); Query OK, 0 rows affected (0.44 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable679 values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable679 values('Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable679 values('David'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable679 values('Bob'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable679 values('Mike'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable679 values('Sam'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable679 values('Robert'); Query OK, 1 row affected (0.15 sec)
使用 select 语句从表中显示所有记录 −
mysql> select *from DemoTable679;
这会产生以下输出 −
+-----------+ | FirstName | +-----------+ | John | | Chris | | David | | Bob | | Mike | | Sam | | Robert | +-----------+ 7 rows in set (0.00 sec)
以下是 MySQL 中选择最后 n 行的查询。此处,我们首先按升序排序,并显示前 4 条记录 −
mysql> select * from DemoTable679 order by FirstName Limit 0,4;
这会产生以下输出 −
+-----------+ | FirstName | +-----------+ | Bob | | Chris | | David | | John | +-----------+ 4 rows in set (0.00 sec)
广告