duck
duck

Reputation: 141

select from index to index

I am building pagination and am not familiar with SQL. I need help to parse an SQL statement.

For example, I have following array:

[1, 2, 3, 4, 5, 6, 7]

By selecting from index 2 to index 4, I get

[3, 4, 5]

To do the similar thing in SQL, for example, this SQL statement:

SELECT * FROM person WHERE birthday BETWEEN '2001-01-01' AND '2020-01-01'

gives the result

{
    "Jason": ...
    "Mary": ...
    "Josh": ...
    "Peter": ...
    "Laren": ...
    "Dan": ...
}

I only want the 3~5th items from the array. which

{
    "Peter": ...
    "Laren": ...
    "Dan": ...
}

To be specific, is there a way I can pass in a starting index and an ending index into the SQL statement to only collect segments of records of certain length?

I don't want to manipulate data in PHP since I worry about RAM and performance. I am a newbie so any suggestion is welcomed.

Upvotes: 1

Views: 96

Answers (1)

sanderbee
sanderbee

Reputation: 703

Take a look at this: https://www.mysqltutorial.org/mysql-limit.aspx

Use 'offset' and 'limit':

SELECT * FROM person WHERE birthday BETWEEN '2001-01-01' AND '2020-01-01' LIMIT 3, 5

Upvotes: 1

Related Questions