在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,这个时候怎么办呢?不用担心,mysql已经为我们提供了上面这样一个功能。
(0)mysql不支持select top n的语法,可是要用mysql实现top n功能时,应该用这个替换:
select * from tablename order by orderfield desc/asc limit position, counter;
position 指示从哪里开始查询,如果是0则是从头开始,counter 表示查询的个数
取前15条记录:
select * from tablename order by orderfield desc/asc limit 0,15
(1)Sql limit操作,用于循环分批量读取并处理数据,避免出现类似于MySQL Server has gone away的问题。
SELECT
*
FROM
table
LIMIT
[
offset,
]
rows
|
rows OFFSET offset
LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1): 为了与 PostgreSQL 兼容,MySQL 也支持句法: LIMIT # OFFSET #。
表示从3行以后开始(不包括第3行),按照数据库顺序取10条数据,即检索得到4到13行的数据。
mysql> select * from auth_permission limit 3,10;
+----+-----------------------+-----------------+-------------------+
| id | name | content_type_id | codename |
+----+-----------------------+-----------------+-------------------+
| 4 | Can add group | 2 | add_group |
| 5 | Can change group | 2 | change_group |
| 6 | Can delete group | 2 | delete_group |
| 7 | Can add permission | 3 | add_permission |
| 8 | Can change permission | 3 | change_permission |
| 9 | Can delete permission | 3 | delete_permission |
| 10 | Can add user | 4 | add_user |
| 11 | Can change user | 4 | change_user |
| 12 | Can delete user | 4 | delete_user |
| 13 | Can add content type | 5 | add_contenttype |
+----+-----------------------+-----------------+-------------------+
10 rows in set (0.00 sec)
SELECT
*
FROM
table
LIMIT
[
offset,
]
rows
|
rows OFFSET offset
mysql
>
SELECT
*
FROM
table
LIMIT
5
,
10
;
//
检索记录行
6
-
15
//
为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为
-
1
:
mysql
>
SELECT
*
FROM
table
LIMIT
95
,
-
1
;
//
检索记录行
96
-
last.
//
如果只给定一个参数,它表示返回最大的记录行数目:
mysql
>
SELECT
*
FROM
table
LIMIT
5
;
//
检索前
5
个记录行
//
换句话说,LIMIT n 等价于 LIMIT
0
,n。
select * from table LIMIT 5,10; #返回第6-15行数据
select * from table LIMIT 5; #返回前5行
select * from table LIMIT 0,5; #返回前5行
//换句话说,LIMIT n 等价于 LIMIT 0,n。
mysql> select * from auth_permission limit 0,5;
+----+----------------------+-----------------+-----------------+
| id | name | content_type_id | codename |
+----+----------------------+-----------------+-----------------+
| 1 | Can add log entry | 1 | add_logentry |
| 2 | Can change log entry | 1 | change_logentry |
| 3 | Can delete log entry | 1 | delete_logentry |
| 4 | Can add group | 2 | add_group |
| 5 | Can change group | 2 | change_group |
+----+----------------------+-----------------+-----------------+
5 rows in set (0.00 sec)
mysql> select * from auth_permission limit 5;
+----+----------------------+-----------------+-----------------+
| id | name | content_type_id | codename |
+----+----------------------+-----------------+-----------------+
| 1 | Can add log entry | 1 | add_logentry |
| 2 | Can change log entry | 1 | change_logentry |
| 3 | Can delete log entry | 1 | delete_logentry |
| 4 | Can add group | 2 | add_group |
| 5 | Can change group | 2 | change_group |
+----+----------------------+-----------------+-----------------+
5 rows in set (0.00 sec)
mysql> select * from auth_permission limit 5,10;
+----+-------------------------+-----------------+--------------------+
| id | name | content_type_id | codename |
+----+-------------------------+-----------------+--------------------+
| 6 | Can delete group | 2 | delete_group |
| 7 | Can add permission | 3 | add_permission |
| 8 | Can change permission | 3 | change_permission |
| 9 | Can delete permission | 3 | delete_permission |
| 10 | Can add user | 4 | add_user |
| 11 | Can change user | 4 | change_user |
| 12 | Can delete user | 4 | delete_user |
| 13 | Can add content type | 5 | add_contenttype |
| 14 | Can change content type | 5 | change_contenttype |
| 15 | Can delete content type | 5 | delete_contenttype |
+----+-------------------------+-----------------+--------------------+
10 rows in set (0.00 sec)
(2)按照id字段,升序排列(id字段从小到大排列)
mysql> select * from auth_permission order by id asc;
相当于如下默认情况
mysql> select * from auth_permission;
+----+-------------------------+-----------------+--------------------+
| id | name | content_type_id | codename |
+----+-------------------------+-----------------+--------------------+
| 1 | Can add log entry | 1 | add_logentry |
| 2 | Can change log entry | 1 | change_logentry |
| 3 | Can delete log entry | 1 | delete_logentry |
| 4 | Can add group | 2 | add_group |
| 5 | Can change group | 2 | change_group |
+----+-------------------------+-----------------+——————————+
(3)按照id字段,升序排列(id字段从大到小排列)
mysql> select * from auth_permission order by id desc;
mysql> select * from auth_permission order by id desc;
+----+-------------------------+-----------------+--------------------+
| id | name | content_type_id | codename |
+----+-------------------------+-----------------+--------------------+
| 5 | Can change group | 2 | change_group |
| 4 | Can add group | 2 | add_group |
| 3 | Can delete log entry | 1 | delete_logentry |
| 2 | Can change log entry | 1 | change_logentry |
| 1 | Can add log entry | 1 | add_logentry |
+----+-------------------------+-----------------+--------------------+
(4)随机从mysql表中获取特定数目的数据
在MySQL 中limit是按顺序取数据的,所以要随机取数据时,使用 order by rand();
mysql> select * from auth_permission limit 5;
+----+----------------------+-----------------+-----------------+
| id | name | content_type_id | codename |
+----+----------------------+-----------------+-----------------+
| 1 | Can add log entry | 1 | add_logentry |
| 2 | Can change log entry | 1 | change_logentry |
| 3 | Can delete log entry | 1 | delete_logentry |
| 4 | Can add group | 2 | add_group |
| 5 | Can change group | 2 | change_group |
+----+----------------------+-----------------+-----------------+
5 rows in set (0.00 sec)
mysql> select * from auth_permission order by rand() limit 5;
+----+----------------------+-----------------+-----------------+
| id | name | content_type_id | codename |
+----+----------------------+-----------------+-----------------+
| 6 | Can delete group | 2 | delete_group |
| 3 | Can delete log entry | 1 | delete_logentry |
| 2 | Can change log entry | 1 | change_logentry |
| 7 | Can add permission | 3 | add_permission |
| 20 | Can change test | 7 | change_test |
+----+----------------------+-----------------+-----------------+
5 rows in set (0.00 sec)
mysql> select * from auth_permission order by rand() limit 5;
+----+-------------------------+-----------------+--------------------+
| id | name | content_type_id | codename |
+----+-------------------------+-----------------+--------------------+
| 10 | Can add user | 4 | add_user |
| 15 | Can delete content type | 5 | delete_contenttype |
| 1 | Can add log entry | 1 | add_logentry |
| 18 | Can delete session | 6 | delete_session |
| 19 | Can add test | 7 | add_test |
+----+-------------------------+-----------------+--------------------+
5 rows in set (0.00 sec)
(5)附录:
给出表结构和表数据:
mysql> show create table auth_permission \G;
*************************** 1. row ***************************
Table: auth_permission
Create Table: CREATE TABLE `auth_permission` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`content_type_id` int(11) NOT NULL,
`codename` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `auth_permission_content_type_id_codename_01ab375a_uniq` (`content_type_id`,`codename`),
CONSTRAINT `auth_permission_content_type_id_2f476e4b_fk_django_co` FOREIGN KEY (`content_type_id`) REFERENCES `django_content_type` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=latin1
1 row in set (0.01 sec)
mysql> select * from auth_permission;
+----+-------------------------+-----------------+--------------------+
| id | name | content_type_id | codename |
+----+-------------------------+-----------------+--------------------+
| 1 | Can add log entry | 1 | add_logentry |
| 2 | Can change log entry | 1 | change_logentry |
| 3 | Can delete log entry | 1 | delete_logentry |
| 4 | Can add group | 2 | add_group |
| 5 | Can change group | 2 | change_group |
| 6 | Can delete group | 2 | delete_group |
| 7 | Can add permission | 3 | add_permission |
| 8 | Can change permission | 3 | change_permission |
| 9 | Can delete permission | 3 | delete_permission |
| 10 | Can add user | 4 | add_user |
| 11 | Can change user | 4 | change_user |
| 12 | Can delete user | 4 | delete_user |
| 13 | Can add content type | 5 | add_contenttype |
| 14 | Can change content type | 5 | change_contenttype |
| 15 | Can delete content type | 5 | delete_contenttype |
| 16 | Can add session | 6 | add_session |
| 17 | Can change session | 6 | change_session |
| 18 | Can delete session | 6 | delete_session |
| 19 | Can add test | 7 | add_test |
| 20 | Can change test | 7 | change_test |
| 21 | Can delete test | 7 | delete_test |
+----+-------------------------+-----------------+——————————+