MySQL查询一次选择一行
为此,可以使用LIMIT和OFFSET的概念。让我们首先创建一个表-
mysql> create table DemoTable1514 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> FirstName varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1514(FirstName) values('Chris'); mysql> insert into DemoTable1514(FirstName) values('Bob'); mysql> insert into DemoTable1514(FirstName) values('Sam'); mysql> insert into DemoTable1514(FirstName) values('Mike'); mysql> insert into DemoTable1514(FirstName) values('Carol'); mysql> insert into DemoTable1514(FirstName) values('David'); mysql> insert into DemoTable1514(FirstName) values('Robert'); mysql> insert into DemoTable1514(FirstName) values('Adam'); mysql> insert into DemoTable1514(FirstName) values('John'); mysql> insert into DemoTable1514(FirstName) values('Jace');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1514;
这将产生以下输出-
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | Chris | | 2 | Bob | | 3 | Sam | | 4 | Mike | | 5 | Carol | | 6 | David | | 7 | Robert | | 8 | Adam | | 9 | John | | 10 | Jace | +----+-----------+ 10 rows in set (0.00 sec)
这是一次选择一批的查询-
mysql> select * from DemoTable1514 limit 4 offset 4;
这将产生以下输出-
+----+-----------+ | Id | FirstName | +----+-----------+ | 5 | Carol | | 6 | David | | 7 | Robert | | 8 | Adam | +----+-----------+ 4 rows in set (0.00 sec)