如何使用 MySQL 查询来选择前 10 条记录?


要选择前 10 条记录,请在 MySQL 中使用 LIMIT。我们先创建一个表 -

mysql> create table DemoTable
-> (
-> PageNumber text
-> );
Query OK, 0 rows affected (2.50 sec)

使用 insert 命令在表中插入一些记录 -

mysql> insert into DemoTable values('Page-1');
Query OK, 1 row affected (0.46 sec)

mysql> insert into DemoTable values('Page-2');
Query OK, 1 row affected (0.11 sec)

mysql> insert into DemoTable values('Page-3');
Query OK, 1 row affected (0.27 sec)

mysql> insert into DemoTable values('Page-4');
Query OK, 1 row affected (0.19 sec)

mysql> insert into DemoTable values('Page-5');
Query OK, 1 row affected (0.27 sec)

mysql> insert into DemoTable values('Page-6');
Query OK, 1 row affected (1.12 sec)

mysql> insert into DemoTable values('Page-7');
Query OK, 1 row affected (1.21 sec)

mysql> insert into DemoTable values('Page-8');
Query OK, 1 row affected (1.15 sec)

mysql> insert into DemoTable values('Page-9');
Query OK, 1 row affected (0.89 sec)

mysql> insert into DemoTable values('Page-10');
Query OK, 1 row affected (1.20 sec)

mysql> insert into DemoTable values('Page-11');
Query OK, 1 row affected (0.86 sec)

mysql> insert into DemoTable values('Page-12');
Query OK, 1 row affected (0.39 sec)

mysql> insert into DemoTable values('Page-13');
Query OK, 1 row affected (0.39 sec)

mysql> insert into DemoTable values('Page-14');
Query OK, 1 row affected (0.47 sec)

使用 select 语句显示表中所有记录 -

mysql> select *from DemoTable;

输出

这将产生以下输出 -

+------------+
| PageNumber |
+------------+
| Page-1     |
| Page-2     |
| Page-3     |
| Page-4     |
| Page-5     |
| Page-6     |
| Page-7     |
| Page-8     |
| Page-9     |
| Page-10    |
| Page-11    |
| Page-12    |
| Page-13    |
| Page-14    |
+------------+
14 rows in set (0.00 sec)

以下是选择前 10 条记录的查询 -

mysql> select *from DemoTable limit 0,10;

输出

这将产生以下输出 -

+------------+
| PageNumber |
+------------+
| Page-1     |
| Page-2     |
| Page-3     |
| Page-4     |
| Page-5     |
| Page-6     |
| Page-7     |
| Page-8     |
| Page-9     |
| Page-10    |
+------------+
10 rows in set (0.00 sec)

更新于:2020-06-30

11K+ 浏览

提升您的职业生涯

完成课程获取认证

入门
广告