Ash
Ash

Reputation: 1309

Selecting some results of SQL query

From a simple SQL query in MySQL how can I get only the 5 first results?
And then, how can I get the next 5 results?

For example (pseudo code):

select * from (select * from some_table) where <first 5 results>
select * from (select * from some_table) where <second 5 results (6-10)>

Upvotes: 0

Views: 206

Answers (2)

Greg Aponte
Greg Aponte

Reputation: 676

You should be able to get the first 5 results with a LIMIT 5 at the end of your statement:

SELECT * FROM some_table LIMIT 5;

And then you can get results 6-10 with a query like this:

SELECT * FROM some_table LIMIT 5 OFFSET 5;

As another example, you could get results 6-15 with a query like this:

SELECT * FROM some_table LIMIT 10 OFFSET 5;

Please keep in mind that, if you don't add an ORDER BY statement, the results are retrieved in arbitrary order. Consequently, it doesn't really make sense to use LIMIT and OFFSET in the absence of an ORDER BY.

Upvotes: 3

Brohan
Brohan

Reputation: 123

You can do this by making a SQL union

select * from (select * from some_table) where <first 5 results>
UNION ALL 
select * from (select * from some_table) where <second 5 results (6-10)>

Upvotes: 0

Related Questions