Home > Database > Mysql Tutorial > How to Get the Top N Rows in MySQL Without the TOP Keyword?

How to Get the Top N Rows in MySQL Without the TOP Keyword?

Mary-Kate Olsen
Release: 2025-01-04 16:27:40
Original
841 people have browsed it

How to Get the Top N Rows in MySQL Without the TOP Keyword?

Alternative to TOP Keyword in MySQL

In MySQL, the TOP keyword, familiar from SQL Server, is not available. However, you can achieve the same functionality using other methods.

Ordering and Limiting Results

The alternative in MySQL is to use the ORDER BY and LIMIT clauses in combination. For example, to retrieve the top 10 rows based on field1 in ascending order:

SELECT field1, field2
FROM myTable
ORDER BY field1 ASC
LIMIT 10;
Copy after login

By adjusting the LIMIT clause, you can retrieve any number of top rows as needed.

The above is the detailed content of How to Get the Top N Rows in MySQL Without the TOP Keyword?. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template