SQL Access 数据库 LIMIT 关键字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4551112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 08:42:36  来源:igfitidea点击:

Access Database LIMIT keyword

sqlms-accessasp-classic

提问by Elliott

I'm trying to get my page listing function working in ASP with an Access database, but I don't know the alternative to LIMIT in Microsoft SQL. I have tried TOP but this doesn't seem to be working.

我正在尝试使用 Access 数据库在 ASP 中使用我的页面列表功能,但我不知道 Microsoft SQL 中 LIMIT 的替代方法。我试过 TOP 但这似乎不起作用。

Here is the statement am using with MySQL:

这是我在 MySQL 中使用的语句:

SELECT  * FROM customers ORDER BY customerName DESC LIMIT 0, 5

How can I convert this to work with Access Database?

如何将其转换为使用 Access 数据库?

回答by dEePaK

According to ms-access view:

根据ms-access的观点:

SELECT TOP(5) * FROM customers ORDER BY customerName; 

will fetch an error "The SELECT statement includes a reserved word",

将获取错误“SELECT 语句包含保留字”,

the correct syntax is:

正确的语法是:

SELECT TOP 5 * FROM customers ORDER BY customerName; 

(note the brackets)..

(注意括号)。

回答by xd9813

Top(5) is deceptive. Internally the database returns all records, then Access just shows the Top 5 rows. I'd use the LIMIT keyword instead of Top(n).

Top(5) 是骗人的。数据库在内部返回所有记录,然后 Access 只显示前 5 行。我会使用 LIMIT 关键字而不是 Top(n)。

回答by Haloeye

There is no direct equivalent in access for LIMIT, but the TOP statement can be manipulated into working in a similar fashion to say, "... LIMIT BY 50, 250" etc,. I found out by experiment that if you wanted to get the "next 50" records at an offset of 250 you could try the following

LIMIT 的访问没有直接的等价物,但是可以操纵 TOP 语句以类似的方式工作,例如“... LIMIT BY 50, 250”等。我通过实验发现,如果您想以 250 的偏移量获得“接下来的 50”条记录,您可以尝试以下操作

SELECT * FROM (SELECT TOP 50 tab2.* FROM (SELECT TOP 300 tab1.* FROM my_table AS tab1 ORDER BY column_name ASC) AS tab2 ORDER BY column_name DESC) ORDER BY column_name ASC;

SELECT * FROM (SELECT TOP 50 tab2.* FROM (SELECT TOP 300 tab1.* FROM my_table AS tab1 ORDER BY column_name ASC) AS tab2 ORDER BY column_name DESC) ORDER BY column_name ASC;

This should return the records from row 250 to 300, in ascending order (provided they exist.) with or without a unique index. A WHERE clause could tidy the results further if need be.

这应该返回从第 250 行到第 300 行的记录,按升序(如果它们存在)。有或没有唯一索引。如果需要,WHERE 子句可以进一步整理结果。

A little convoluted but I hope it helps.

有点复杂,但我希望它有所帮助。