MySQL 如何在MySQL中从第x行开始获取所有行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1492297/
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-08-31 14:11:25  来源:igfitidea点击:

How to get ALL rows starting from row x in MySQL

mysqllimitoffset

提问by SolidSmile

In MySQL, how can I retrieve ALL rows in a table, starting from row X? For example, starting from row 6:

在 MySQL 中,如何从 X 行开始检索表中的所有行?例如,从第 6 行开始:

LIMIT 5,0

This returns nothing, so I tried this:

这没有任何回报,所以我试过这个:

LIMIT 5,ALL

Still no results (sql error).

仍然没有结果(sql错误)。

I'm not looking for pagination functionality, just retrieving all rows starting from a particular row. LIMIT 5,2000seems like overkill to me. Somehow Google doesn't seem to get me some answers. Hope you can help.

我不是在寻找分页功能,只是检索从特定行开始的所有行。LIMIT 5,2000对我来说似乎有点矫枉过正。不知何故,谷歌似乎没有给我一些答案。希望你能帮忙。

Thanks

谢谢

回答by Greg

According to the documentation:

根据文档

To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:

要检索从某个偏移量到结果集末尾的所有行,您可以为第二个参数使用一些大数字。此语句检索从第 96 行到最后一行的所有行:

SELECT * FROM tbl LIMIT 95, 18446744073709551615;

This is the maximum rows a MyISAM table can hold, 2^64-1.

这是MyISAM 表可以容纳最大行数,2^64-1。

There is a limit of 2^32 (~4.295E+09) rows in a MyISAM table. If you build MySQL with the --with-big-tables option, the row limitation is increased to (2^32)^2 (1.844E+19) rows. See Section 2.16.2, “Typical configure Options”. Binary distributions for Unix and Linux are built with this option.

MyISAM 表中有 2^32 (~4.295E+09) 行的限制。如果使用 --with-big-tables 选项构建 MySQL,行限制将增加到 (2^32)^2 (1.844E+19) 行。请参见第 2.16.2 节,“典型配置选项”。Unix 和 Linux 的二进制发行版就是用这个选项构建的。

回答by dnagirl

If you're looking to get the last x number of rows, the easiest thing to do is SORT DESCand LIMIT to the firstx rows. Granted, the SORTwill slow your query down. But if you're opposed to setting an arbitrarily large number as the second LIMITarg, then that's the way to do it.

如果您想获得最后 x 行,最简单的方法是SORT DESCLIMIT 到x 行。当然,这SORT会减慢您的查询速度。但是,如果您反对将任意大的数字设置为第二个LIMIT参数,那么这就是这样做的方法。

回答by Alistair Evans

The only solution I am aware of currently is to do as you say and give a ridiculously high number as the second argument to LIMIT. I do not believe there is any difference in performance to specifying a low number or a high number, mysql will simply stop returning rows at the end of the result set, or when it hits your limit.

我目前知道的唯一解决方案是按照你说的做,并给出一个高得离谱的数字作为 LIMIT 的第二个参数。我不认为指定低数或高数在性能上有任何区别,mysql 将在结果集末尾或达到您的限制时停止返回行。

回答by Javid Ahadov

I think you don't need to enter max value for select all by LIMIT. It is enough to find count of table and then use it as max LIMIT.

我认为您不需要为 LIMIT 全选输入最大值。找到表的数量然后将其用作最大 LIMIT 就足够了。

回答by Jake

The next query should work too, and is in my opinion more effective...

下一个查询也应该有效,并且在我看来更有效......

SELECT * FROM mytbl WHERE id != 1 ORDER BY id asc

By ordering the query will find the id imediately and skip this one, so the next rows he won't check anymore whether the id = 1.

通过排序查询将立即找到 id 并跳过这一行,因此他将不再检查下一行是否 id = 1。