Home >Database >Mysql Tutorial >How to Select Rows within a Specific ROWNUM Range in SQL?

How to Select Rows within a Specific ROWNUM Range in SQL?

Barbara Streisand
Barbara StreisandOriginal
2025-01-05 17:18:43760browse

How to Select Rows within a Specific ROWNUM Range in SQL?

Retrieving Rows within a Specified ROWNUM Range

Question:

How can you select rows with ROWNUM values falling within a specific range?

An attempt was made using the following code:

select * from maps006 where rownum >49 and rownum <101

However, this query only retrieves rows where the ROWNUM value is less than the specified threshold.

Answer:

To correctly retrieve rows within a ROWNUM range, you need to utilize a subquery:

SELECT * from
(
select m.*, rownum r
from maps006 m
)
where r > 49 and r < 101

This subquery assigns a new column named "r" that represents the ROWNUM value for each row in the "maps006" table. You can then apply the range conditions to this column to retrieve the desired rows:

r > 49

oznacza numer wiersza większy niż 49, a

r < 101

oznacza numer wiersza mniejszy niż 101. Połączenie tych warunków gwarantuje, że zostaną wybrane tylko wiersze z numerami wierszy w zakresie od 50 do 100.

The above is the detailed content of How to Select Rows within a Specific ROWNUM Range in SQL?. For more information, please follow other related articles on the PHP Chinese website!

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