Range number of rows in MySql

MySql limit is used to range number of rows in a table to be displayed. When Limit used with two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

Display all rows in a table

mysql> select * from employee;
+-------+--------+----------+------+------------+------+-------+--------+
| EMPNO | ENAME  | JOB      | MGR  | HIREDATE   | SAL  | COMM  | DEPTNO |
+-------+--------+----------+------+------------+------+-------+--------+
|  7369 | Smith  | Clerk    | 7902 | 1980-12-17 | 8000 |     0 |     20 |
|  7499 | ALLEN  | SALESMAN | 7698 | 1981-06-20 |  600 |   300 |     30 |
|  7521 | WARD   | SALESMAN | 7698 | 1981-04-22 | 1250 |   500 |     30 |
|  7566 | JONES  | MANAGER  | 7839 | 1981-02-02 | 2975 | 53269 |     20 |
|  7654 | MARTIN | SALESMAN | 7698 | 1981-05-28 | 1250 |  4521 |     30 |
|  7698 | BLAKE  | MANAGER  | 7839 | 1981-06-01 | 2850 |  2000 |     30 |
|  7782 | CLARK  | MANAGER  | 7839 | 1981-03-09 | 2450 |  3322 |     10 |
|  7788 | SCOTT  | ANALYST  | 7566 | 1982-08-09 | 3000 |  5555 |     20 |
+-------+--------+----------+------+------------+------+-------+--------+
8 rows in set (0.00 sec)

Limit rows with two parameters

mysql> # start with row 3 (0,1,2) and display next 3 records
mysql> select * from employee limit 2,3;
+-------+--------+----------+------+------------+------+-------+--------+
| EMPNO | ENAME  | JOB      | MGR  | HIREDATE   | SAL  | COMM  | DEPTNO |
+-------+--------+----------+------+------------+------+-------+--------+
|  7521 | WARD   | SALESMAN | 7698 | 1981-04-22 | 1250 |   500 |     30 |
|  7566 | JONES  | MANAGER  | 7839 | 1981-02-02 | 2975 | 53269 |     20 |
|  7654 | MARTIN | SALESMAN | 7698 | 1981-05-28 | 1250 |  4521 |     30 |
+-------+--------+----------+------+------------+------+-------+--------+
3 rows in set (0.00 sec)
Share

You may also like...