MySQL之limit使用

如emp表,mysql

複製代碼
mysql> select * from emp;

+-----+--------+-----+--------+--------+----------+---------------------+---------------------+
| eid | ename  | sex | salary | deptid | leaderid | birdate             | income              |
+-----+--------+-----+--------+--------+----------+---------------------+---------------------+
|   1 | 馬雲   | 男  |      1 |   1000 |        0 | 1985-03-05 00:00:00 | 2008-05-06 00:00:00 |
|   2 | 李小龍 | 男  |  20000 |   1001 |        1 | 1983-06-07 00:00:00 | 2012-05-05 00:00:00 |
|   3 | 成龍   | 男  |  23000 |   1002 |        2 | 1973-06-07 00:00:00 | 2013-05-05 00:00:00 |
|   4 | 楊明   | 男  |  22000 |   1003 |        2 | 1993-06-09 00:00:00 | 2013-05-06 00:00:00 |
|   5 | 黎明   | 男  |  28000 |   1004 |        3 | 1992-08-09 00:00:00 | 2013-07-06 00:00:00 |
|   6 | 范冰冰 | 女  |  12000 |   1005 |        4 | 1994-08-09 00:00:00 | 2014-07-06 00:00:00 |
|   7 | 劉亦菲 | 女  |  19000 |   1006 |        5 | 1997-08-09 00:00:00 | 2011-07-03 00:00:00 |
|   8 | 唐嫣   | 女  |  39000 |   1007 |        6 | 1991-08-09 00:00:00 | 2015-07-03 00:00:00 |
+-----+--------+-----+--------+--------+----------+---------------------+---------------------+
複製代碼

拿表中 1 條數據sql

mysql> select * from emp limit 1;
+-----+-------+-----+--------+--------+----------+---------------------+---------------------+
| eid | ename | sex | salary | deptid | leaderid | birdate             | income              |
+-----+-------+-----+--------+--------+----------+---------------------+---------------------+
|   1 | 馬雲  | 男  |      1 |   1000 |        0 | 1985-03-05 00:00:00 | 2008-05-06 00:00:00 |
+-----+-------+-----+--------+--------+----------+---------------------+---------------------+

或者spa

複製代碼
mysql> select * from emp limit 0,1
;
+-----+-------+-----+--------+--------+----------+---------------------+---------------------+
| eid | ename | sex | salary | deptid | leaderid | birdate             | income              |
+-----+-------+-----+--------+--------+----------+---------------------+---------------------+
|   1 | 馬雲  | 男  |      1 |   1000 |        0 | 1985-03-05 00:00:00 | 2008-05-06 00:00:00 |
+-----+-------+-----+--------+--------+----------+---------------------+---------------------+


mysql> select * from emp limit 1,1
;
+-----+--------+-----+--------+--------+----------+---------------------+---------------------+
| eid | ename  | sex | salary | deptid | leaderid | birdate             | income              |
+-----+--------+-----+--------+--------+----------+---------------------+---------------------+
|   2 | 李小龍 | 男  |  20000 |   1001 |        1 | 1983-06-07 00:00:00 | 2012-05-05 00:00:00 |
+-----+--------+-----+--------+--------+----------+---------------------+---------------------+
複製代碼

拿其中3條數據code

複製代碼
mysql> select * from emp limit 2,3;
+-----+-------+-----+--------+--------+----------+---------------------+---------------------+
| eid | ename | sex | salary | deptid | leaderid | birdate             | income              |
+-----+-------+-----+--------+--------+----------+---------------------+---------------------+
|   3 | 成龍  | 男  |  23000 |   1002 |        2 | 1973-06-07 00:00:00 | 2013-05-05 00:00:00 |
|   4 | 楊明  | 男  |  22000 |   1003 |        2 | 1993-06-09 00:00:00 | 2013-05-06 00:00:00 |
|   5 | 黎明  | 男  |  28000 |   1004 |        3 | 1992-08-09 00:00:00 | 2013-07-06 00:00:00 |
+-----+-------+-----+--------+--------+----------+---------------------+---------------------+
複製代碼

拿其中4條數據blog

複製代碼
mysql> select * from emp limit 2,4;
+-----+--------+-----+--------+--------+----------+---------------------+---------------------+
| eid | ename  | sex | salary | deptid | leaderid | birdate             | income              |
+-----+--------+-----+--------+--------+----------+---------------------+---------------------+
|   3 | 成龍   | 男  |  23000 |   1002 |        2 | 1973-06-07 00:00:00 | 2013-05-05 00:00:00 |
|   4 | 楊明   | 男  |  22000 |   1003 |        2 | 1993-06-09 00:00:00 | 2013-05-06 00:00:00 |
|   5 | 黎明   | 男  |  28000 |   1004 |        3 | 1992-08-09 00:00:00 | 2013-07-06 00:00:00 |
|   6 | 范冰冰 | 女  |  12000 |   1005 |        4 | 1994-08-09 00:00:00 | 2014-07-06 00:00:00 |
+-----+--------+-----+--------+--------+----------+---------------------+---------------------+
複製代碼

拿 5條數據it

複製代碼
mysql> select * from emp limit 1,5;
+-----+--------+-----+--------+--------+----------+---------------------+---------------------+
| eid | ename  | sex | salary | deptid | leaderid | birdate             | income              |
+-----+--------+-----+--------+--------+----------+---------------------+---------------------+
|   2 | 李小龍 | 男  |  20000 |   1001 |        1 | 1983-06-07 00:00:00 | 2012-05-05 00:00:00 |
|   3 | 成龍   | 男  |  23000 |   1002 |        2 | 1973-06-07 00:00:00 | 2013-05-05 00:00:00 |
|   4 | 楊明   | 男  |  22000 |   1003 |        2 | 1993-06-09 00:00:00 | 2013-05-06 00:00:00 |
|   5 | 黎明   | 男  |  28000 |   1004 |        3 | 1992-08-09 00:00:00 | 2013-07-06 00:00:00 |
|   6 | 范冰冰 | 女  |  12000 |   1005 |        4 | 1994-08-09 00:00:00 | 2014-07-06 00:00:00 |
+-----+--------+-----+--------+--------+----------+---------------------+---------------------+
複製代碼

總結:  limit後面能夠接一個數字,表示拿幾條數據,默認從第一條開始拿; limit後面能夠接兩個數字,第一個數字表明 從該數字起, 第二個數字表明 拿多少條。class

相關文章
相關標籤/搜索