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

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

WBOY
WBOYOriginal
2016-06-01 14:06:221248browse

MySQL查询

MySQL中,如何实现Top NMN段的记录查询?

我们可以利用MySQLSELECT支持的一个子句——LIMIT——来完成这项功能。

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

SELECT * FROM MYTABLE
ORDER BY AFIELD

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

select * from mytable
order by afield

limit 2, 5

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

Statement:
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