행을 건너뛰려면 OFFSET을 사용할 수 있습니다. 먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable617(Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,FirstName varchar(100)); Query OK, 0 rows affected (1.70 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable617(FirstName) values('John'); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable617(FirstName) values('Sam'); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable617(FirstName) values('Carol'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable617(FirstName) values('Mike'); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable617(FirstName) values('David'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable617(FirstName) values('Bob'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable617(FirstName) values('Robert'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable617(FirstName) values('Chris'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable617(FirstName) values('Mike'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable617(FirstName) values('Adam'); Query OK, 1 row affected (0.49 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select *from DemoTable617;
이것은 다음과 같은 출력을 생성합니다 -
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | John | | 2 | Sam | | 3 | Carol | | 4 | Mike | | 5 | David | | 6 | Bob | | 7 | Robert | | 8 | Chris | | 9 | Mike | | 10| Adam | +----+-----------+ 10 rows in set (0.00 sec)
다음은 행을 선택하는 쿼리입니다. DESC의 첫 번째 주문에서. 그 후 LIMIT 5 −
를 사용하여 두 행을 건너뛰고 5행을 표시합니다.mysql> select tbl.* from (select *from DemoTable617 order by FirstName DESC LIMIT 5 OFFSET 2) AS tbl ORDER BY tbl.Id ;
이것은 다음과 같은 출력을 생성합니다 -
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | John | | 4 | Mike | | 5 | David | | 8 | Chris | | 9 | Mike | +----+-----------+ 5 rows in set (0.00 sec)