在 MySQL 中抓取两行之间的数据?
要抓取两行之间的数据,需利用 LIMIT 概念。我们首先创建一个表 −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(10) ); Query OK, 0 rows affected (0.22 sec)
使用插入命令在表内插入一些记录 −
mysql> insert into DemoTable(Name) values('Larry'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Name) values('Robert'); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable(Name) values('David'); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(Name) values('Bob'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Name) values('Sam'); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(Name) values('Mike'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Name) values('Carol'); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.05 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将生成以下输出 −
+----+--------+ | Id | Name | +----+--------+ | 1 | Larry | | 2 | Robert | | 3 | Chris | | 4 | David | | 5 | Bob | | 6 | Sam | | 7 | Mike | | 8 | Carol | | 9 | John | +----+--------+ 9 rows in set (0.00 sec)
以下查询可抓取两行之间的数据 −
mysql> select *from DemoTable limit 2,3;
这将生成以下输出 -
+----+-------+ | Id | Name | +----+-------+ | 3 | Chris | | 4 | David | | 5 | Bob | +----+-------+ 3 rows in set (0.00 sec)
广告