MySQL 查询选择太多行?
mysqlmysqli database更新于 2024/12/28 22:04:17
您可以使用 LIMIT 来实现这一点,它用于获取有限数量的记录。让我们首先创建一个表 −
mysql> create table DemoTable ( Id int, Name varchar(20) ); Query OK, 0 rows affected (0.53 sec)
使用 insert 命令在表中插入记录 −
mysql> insert into DemoTable values(10,'John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(11,'Chris'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(12,'David'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(13,'Carol'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(14,'Mike'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(15,'Sam'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(16,'Robert'); Query OK, 1 row affected (0.14 sec)
使用 select 命令显示表中的记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+------+--------+ | Id | Name | +------+--------+ | 10 | John | | 11 | Chris | | 12 | David | | 13 | Carol | | 14 | Mike | | 15 | Sam | | 16 | Robert | +------+--------+ 7 rows in set (0.00 sec)
现在让我们选择太多行。以下查询将跳过前 2 行并返回 4 行 −
mysql> select *from DemoTable limit 2,4;
这将产生以下输出 −
+------+-------+ | Id | Name | +------+-------+ | 12 | David | | 13 | Carol | | 14 | Mike | | 15 | Sam | +------+-------+ 4 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

