php mysql:从 DESC LIMIT 中选择最后 10 行

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

mysql: select last 10 rows from DESC LIMIT

phpmysql

提问by teslasimus

i'm working on a block which will display last 10 posts, when user clicks on "load more" button i would like to display 10 older posts.

我正在处理一个将显示最后 10 个帖子的块,当用户单击“加载更多”按钮时,我想显示 10 个较旧的帖子。

how can i select last 10 rows if i'm already using DESC LIMIT?

如果我已经在使用 DESC LIMIT,我该如何选择最后 10 行?

mysql_query("SELECT title,id,alt_name FROM dle_post WHERE approve='1' AND date >= '$monthagodate'
AND date < '$curdate' + INTERVAL 1 day ORDER BY date DESC LIMIT $more;");

回答by Patrick Moore

You are LIMITing using just one parameter. But you can use LIMIT x,yto specify both x(the position of the first record to return) and y(the number of records to return).

您仅使用一个参数进行限制。但是您可以LIMIT x,y同时指定x(要返回的第一条记录的位置)和y(要返回的记录数)。

Pass a variable to the page like results.php?start=xto set a starting position.

将变量传递给页面,就像results.php?start=x设置起始位置一样。

if (!isset( $_REQUEST['start'] ) ) { $start = 0; } else { $start = (int)$_REQUEST['start']; }

mysql_query("SELECT title,id,alt_name FROM dle_post WHERE approve='1' AND date >= '$monthagodate'
AND date < '$curdate' + INTERVAL 1 day ORDER BY date DESC LIMIT $start,10;");

And then you can generate a link to the next page like so:

然后你可以像这样生成一个指向下一页的链接:

echo '<a href="results.php?start=' . ($_REQUEST['start']+10) . '">Next 10 results</a>';

echo '<a href="results.php?start=' . ($_REQUEST['start']+10) . '">Next 10 results</a>';

回答by Deepak

You should add another parameter $startand use the following query

您应该添加另一个参数$start并使用以下查询

mysql_query("SELECT title,id,alt_name FROM dle_post WHERE approve='1' AND date >= '$monthagodate' AND date < '$curdate' + INTERVAL 1 day ORDER BY date DESC LIMIT $start, $more;");

Here $startis used to tel MySQLthe starting point of records to be fetched.

这里$start用来MySQL告诉要获取的记录的起点。