Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room - Is it possible to use OFFSET and FETCH NEXT in a query?

I'm trying to implement a pagination build in Room database android, and I need to have the clause OFFSET and FETCH NEXT which is not alowing me to use, what's the way to implement a query using these clauses. Did some research but no results gained. The SQL query I'm trying to implement on ROOM dao looks like this:

SELECT * FROM Persons ORDER BY id OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;
like image 275
Taulant Loshi Avatar asked Jun 19 '18 12:06

Taulant Loshi


People also ask

Can we use fetch without offset?

You can use OFFSET without FETCH, but FETCH can't be used by itself. Regardless, OFFSET must be used with an ORDER BY clause. The reason is simple as OFFSET and FETCH are part of the ORDER BY clause. In this example the first ten rows of the result are skipped, then the next 10 displayed in the result.

Can we use limit and offset without ORDER BY?

you can use limit without order by.

Can we use top with offset in SQL Server?

OFFSET and FETCH return the set of records in a query. OFFSET is used to skip the number of rows, and FETCH provides the number of rows we want to return after the OFFSET in the result. TOP is not ANSI-compliant, so it can only be used with Microsoft products like SQL Server and MS-Access.


1 Answers

This is how I managed to achieve and it's working just fine:

(Kotlin sample)

SELECT * FROM Persons WHERE name LIKE :param LIMIT :pageSize OFFSET :pageIndex

where:

  • PARAM = search criteria,

  • PAGESIZE = number of rows to return per page,

  • PAGEINDEX = what page to return

like image 78
Taulant Loshi Avatar answered Nov 12 '22 05:11

Taulant Loshi