MS Access LIMIT X, Y

2019-01-12 04:48发布

问题:

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?

回答1:

While the Access/JET TOP keyword does not directly provide an OFFSET capability, we can use a clever combination of TOP, 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...

SELECT Person.*
FROM Person
WHERE Person.Id In 
      (
        SELECT TOP 10 A.Id
        FROM [
               SELECT TOP 30 Person.Name, Person.Id
               FROM Person
               ORDER BY Person.Name, Person.Id
             ]. AS A
        ORDER BY A.Name DESC, A.Id DESC
      )
ORDER BY Person.Name, Person.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 the PRIMARY KEY.



回答2:

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

SELECT *
    FROM table1 LIMIT 1000, 1999;

MS Access

SELECT TOP 1000 * 
FROM table1 
Where ID NOT IN (SELECT TOP 999 table1.ID FROM table1);

To break this down

SELECT TOP NumA * 
FROM table1 
Where ID NOT IN (SELECT TOP NumB table1.ID FROM table1);

UpperLimit = 1999

LowerLimit = 1000

NumA = UpperLimit - LowerLimit + 1

ex. 1000 = 1999 - 1000 + 1

NumB = LowerLimit -1

ex. 999 = 1000 - 1



回答3:

A better query would be:

SELECT Users.*
FROM Users
WHERE Users.id In 
      (
        SELECT TOP X A.id
        FROM [
               SELECT TOP Y Users.*
               FROM Users
               ORDER BY Users.reg_date DESC
             ]. AS A
        ORDER BY A.reg_date ASC
      )
ORDER BY Users.reg_date DESC

Where

if((totalrows - offset) < limit) then
    X = (totalrows - offset)
else
    X = limit

And:

Y = limit + offset

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



回答4:

Simple and fastest solution.

myTable {ID*, Field2, Filed3...}


  1. Assume your SortOrder contain primary KEY only

    SELECT TOP PageItemsCount tb01.*
    FROM myTable AS tb01
     LEFT JOIN (
      SELECT TOP OffsetValue ID FROM myTable ORDER BY ID ASC
     ) AS tb02
    
     ON tb01.ID = tb02.ID
     WHERE ISNULL(tb02.ID)
     ORDER BY tb01.ID ASC
    

  1. SortOrder based on other fields with duplicated values, in this case you must include your primary key in SortOrder as last one.

For exemple, myTable

+-------+--------+--------+
| ID    | Field2 | Filed3 |
+-------+--------+--------+
| 1     | a1     | b      |
| 2     | a      | b2     |
| 3     | a1     | b2     |
| 4     | a1     | b      |
+-------+--------+--------+

SELECT TOP 2 * From myTable ORDER BY FIELD2;
+-------+--------+--------+
| ID    | Field2 | Filed3 |
+-------+--------+--------+
| 2     | a      | b2     |
| 4     | a1     | b      |
| 3     | a1     | b2     |
| 1     | a1     | b      |
+-------+--------+--------+
SELECT TOP 2 * From myTable ORDER BY FIELD2, FIELD3;
+-------+--------+--------+
| ID    | Field2 | Filed3 |
+-------+--------+--------+
| 2     | a      | b2     |
| 4     | a1     | b      |
| 1     | a1     | b      |
+-------+--------+--------+

But if we add ID to sort order [AS LAST IN FIELDS LIST]

SELECT TOP 2 * From myTable ORDER BY FIELD2, ID;
+-------+--------+--------+
| ID    | Field2 | Filed3 |
+-------+--------+--------+
| 2     | a      | b2     |
| 1     | a1     | b      |
+-------+--------+--------+

Final request

      SELECT TOP PageItemsCount tb01.*
        FROM myTable AS tb01

        LEFT JOIN (
          SELECT TOP OffsetValue ID FROM myTable ORDER BY Field2 ASC, ID
        ) AS tb02

        ON tb01.ID = tb02.ID
        WHERE ISNULL(tb02.ID)
        ORDER BY tb01.Field2 ASC, tb01.ID


回答5:

You can definitely get the the equivalent of "Limit" using the top keyword. See:
Access Database LIMIT keyword



回答6:

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 MySQL LIMIT keyword (See this question).



标签: ms-access