开发者

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)>


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.


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)>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜