Home > Database > Mysql Tutorial > MySQL如何实现Top N及M至N段的记录查询?_MySQL

MySQL如何实现Top N及M至N段的记录查询?_MySQL

WBOY
Release: 2016-06-01 13:55:32
Original
1257 people have browsed it

在MySQL中,如何实现Top N及M至N段的记录查询? 

我们可以利用MySQL中SELECT支持的一个子句――LIMIT――来完成这项功能。

LIMIT可以实现top N查询,也可以实现M至N(某一段)的记录查询,具体语法如下:

SELECT * FROM MYTABLE
ORDER BY AFIELD

LIMIT offset, recnum
其中offset为从第几条(M+1)记录开始,recnum为返回的记录条数。例:

select * from mytable
order by afield

limit 2, 5

即意为从第3条记录开始的5条记录。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template