Is it possible to emulate the following MySQL query:
SELECT * FROM `tbl` ORDER BY `date` DESC LIMIT X, 10
(X is a parameter)
in MS Access?
Is it possible to emulate the following MySQL query:
SELECT * FROM `tbl` ORDER BY `date` DESC LIMIT X, 10
(X is a parameter)
in MS Access?
You can definitely get the the equivalent of "Limit" using the top keyword. See:
Access Database LIMIT keyword
No, JET SQL does not have a direct equivalent. As a workaround, you could add a
WHERE
clause that selects an ordered/id column between two values.If possible, you can also use pass-through queries to an existing MySQL/other database.
While
TOP
in MS-Access can limit records returned, it does not take two parameters as with the MySQLLIMIT
keyword (See this question).While the Access/JET
TOP
keyword does not directly provide anOFFSET
capability, we can use a clever combination ofTOP
, a subquery, and a "derived table" to obtain the same result.Here is an example for getting the 10 rows starting from offset 20 in a Person table in
ORDER BY
Name and Id...Essentially, we query the top 30, reverse the order, query the top 10, and then select the rows from the table that match, sorting in forward order again. This should be fairly efficient, assuming the Id is the
PRIMARY KEY
, and there is an index on Name. It might be that a specific covering index on Name, Id (rather than one on just Name) would be needed for best performance, but I think that indexes implicitly cover thePRIMARY KEY
.Another way - Let say you want from 1000 to 1999 records in a table called table1 (of course if you have that many records) you can do something like this.
MSSQL
MS Access
To break this down
ex. 1000 = 1999 - 1000 + 1
ex. 999 = 1000 - 1
Simple and fastest solution.
Assume your SortOrder contain primary KEY only
For exemple, myTable
Final request
A better query would be:
Where
And:
For example, if total_rows = 12, and we set the limit to 10 (show 10 users per page), and the offset is calculated as p * limit - (limit) where p is the number of the current page, hence in the first page (p = 1) we will get: X = 12 and Y = 10, on the second X = 2 and Y = 20. The list of users is ordered by registration date (descending).