Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite3 (or general SQL) retrieve nth row of a query result

Tags:

sql

sqlite

mysql

Quicky question on SQLite3 (may as well be general SQLite)

How can one retrieve the n-th row of a query result?

row_id (or whichever index) won't work on my case, given that the tables contain a column with a number. Based on some data, the query needs the data unsorted or sorted by asc/desc criteria.

But I may need to quickly retrieve, say, rows 2 & 5 of the results.

So other than implementing a sqlite3_step()==SQLITE_ROW with a counter, right now I have no idea on how to proceed with this.

And I don't like this solution very much because of performance issues.
So, if anyone can drop a hint that'd be highly appreciated.

Regards david

like image 233
David Homes Avatar asked Aug 05 '10 22:08

David Homes


People also ask

How do you find the nth row in SQL?

ROW_NUMBER (Window Function) ROW_NUMBER (Window Function) is a standard way of selecting the nth row of a table. It is supported by all the major databases like MySQL, SQL Server, Oracle, PostgreSQL, SQLite, etc.

How do I select every nth row in SQL?

Here's the SQL query to select every nth row in MySQL. mysql> select * from table_name where table_name.id mod n = 0; In the above query, we basically select every row whose id mod n value evaluates to zero.

How do I select a specific row number in SQL?

For SQL Server, a generic way to go by row number is as such: SET ROWCOUNT @row --@row = the row number you wish to work on.


1 Answers

add LIMIT 1 and OFFSET <n> to the query
example SELECT * FROM users LIMIT 1 OFFSET 5132;

like image 85
wlk Avatar answered Oct 17 '22 00:10

wlk