mysql 數據操作 單表查詢 limit 限制查詢的記錄數


 

 

 

mysql> select * from employee limit 3;
+----+-----------+------+-----+------------+---------+--------------+------------+--------+-----------+
| id | name      | sex  | age | hire_date  | post    | post_comment | salary     | office | depart_id |
+----+-----------+------+-----+------------+---------+--------------+------------+--------+-----------+
|  1 | alex      | male |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
|  2 | yuanhao   | male |  73 | 2014-07-01 | teacher | NULL         |    3500.00 |    401 |         1 |
|  3 | liwenzhou | male |  28 | 2012-11-01 | teacher | NULL         |    2100.00 |    401 |         1 |
+----+-----------+------+-----+------------+---------+--------------+------------+--------+-----------+
3 rows in set (0.00 sec)

 

找出工資最高的那個人詳細信息

mysql> select * from employee order by salary desc limit 1;
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
| id | name | sex  | age | hire_date  | post    | post_comment | salary     | office | depart_id |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
|  1 | alex | male |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
1 row in set (0.00 sec)

 

limit 還有分頁功能

 

 

mysql> select * from employee  limit 0,5;
+----+------------+--------+-----+------------+---------+--------------+------------+--------+-----------+
| id | name       | sex    | age | hire_date  | post    | post_comment | salary     | office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+------------+--------+-----------+
|  1 | alex       | male   |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
|  2 | yuanhao    | male   |  73 | 2014-07-01 | teacher | NULL         |    3500.00 |    401 |         1 |
|  3 | liwenzhou  | male   |  28 | 2012-11-01 | teacher | NULL         |    2100.00 |    401 |         1 |
|  4 | jingliyang | female |  18 | 2011-02-11 | teacher | NULL         |    9000.00 |    401 |         1 |
|  5 | jinxin     | male   |  18 | 1900-03-01 | teacher | NULL         |   30000.00 |    401 |         1 |
+----+------------+--------+-----+------------+---------+--------------+------------+--------+-----------+
5 rows in set (0.00 sec)

mysql> select * from employee  limit 5,5;
+----+--------+--------+-----+------------+---------+--------------+----------+--------+-----------+
| id | name   | sex    | age | hire_date  | post    | post_comment | salary   | office | depart_id |
+----+--------+--------+-----+------------+---------+--------------+----------+--------+-----------+
|  6 | 成龍   | male   |  48 | 2010-11-11 | teacher | NULL         | 10000.00 |    401 |         1 |
|  7 | 歪歪   | female |  48 | 2015-03-11 | sale    | NULL         |  3000.13 |    402 |         2 |
|  8 | 丫丫   | female |  38 | 2010-11-01 | sale    | NULL         |  2000.35 |    402 |         2 |
|  9 | 丁丁   | female |  18 | 2011-03-12 | sale    | NULL         |  1000.37 |    402 |         2 |
| 10 | 星星   | female |  18 | 2016-05-13 | sale    | NULL         |  3000.29 |    402 |         2 |
+----+--------+--------+-----+------------+---------+--------------+----------+--------+-----------+
5 rows in set (0.00 sec)

mysql> select * from employee  limit 10,5;
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| id | name      | sex    | age | hire_date  | post      | post_comment | salary   | office | depart_id |
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| 11 | 格格      | female |  28 | 2017-01-27 | sale      | NULL         |  4000.33 |    402 |         2 |
| 12 | 張野      | male   |  28 | 2016-03-11 | operation | NULL         | 10000.13 |    403 |         3 |
| 13 | 程咬金    | male   |  18 | 1997-03-12 | operation | NULL         | 20000.00 |    403 |         3 |
| 14 | 程咬銀    | female |  18 | 2013-03-11 | operation | NULL         | 19000.00 |    403 |         3 |
| 15 | 程咬銅    | male   |  18 | 2015-04-11 | operation | NULL         | 18000.00 |    403 |         3 |
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
5 rows in set (0.00 sec)

 

 

 

limit 0,5

 

0 從第幾條記錄開始取

 

5 往后一次取多少條記錄

 

 

示例: SELECT * FROM employee ORDER BY salary DESC LIMIT 3; #默認初始位置為0 SELECT * FROM employee ORDER BY salary DESC LIMIT 0,5; #從第0開始,即先查詢出第一條,然后包含這一條在內往后查5條 SELECT * FROM employee ORDER BY salary DESC LIMIT 5,5; #從第5開始,即先查詢出第6條,然后包含這一條在內往后查5條

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM