简体   繁体   中英

SQL select certain number of rows

Hello I need a SQL query statement that gets me rows 'start' to 'finish'.

For example:

A website with many items where page 1 selects only items 1-10, page 2 has 11-20 and so on.

I know how to do this with Microsoft SQL Server and MySQL but I need an implementation that is platform independent. :/

I have an Increment line for IDs but deleting in-between will mess the result when I select via

WHERE ID > number AND ID < othernumber

of course

Is this possible without fetching the whole database to a ResultSet?

I think your safest bet would be to use the BETWEEN operator. I believe it works across Oracle/MySQL/MSSQL.

WHERE ID BETWEEN number AND othernumber

Concerning your comment " I was just think for the case when first 100 IDs are gone I'll have to check further until there is something to fetch", you might wanna consider NOT actually ever deleting stuff from your database but to add a flag like "active" or something like that to your tables so you can avoid situations like the one you're now trying to avoid. The alternative is where you are now, having to find the max and min rows in a filter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM