MySQL之limit使用
如emp表,
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 条数据
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 |
+-----+-------+-----+--------+--------+----------+---------------------+---------------------+
或者
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条数据
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条数据
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条数据
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后面可以接两个数字,第一个数字代表 从该数字起, 第二个数字代表 拿多少条。
每天一点点,感受自己存在的意义。