MySQL 如何从mysql中的表中选择最后N条记录

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

How to select last N records from a table in mysql

mysql

提问by Murthy

This code can be used to select the first ten records from a table in mysql. How can I do the same to display last ten records from a table which has 1000 records. I want to display the name in asc order dont want to change that.

此代码可用于从 mysql 中的表中选择前十条记录。我如何才能显示具有 1000 条记录的表中的最后 10 条记录。我想以 asc 顺序显示名称不想改变它。

   SELECT name, cost FROM test orderby name asc LIMIT 10 ;

回答by Joe Stefanelli

SELECT q.name, q.cost
    FROM (SELECT name, cost
              FROM test
              ORDER BY name DESC LIMIT 10) q
    ORDER BY q.name ASC;

回答by danielrsmith

The LIMITclause can take two parameters, which will provide an offset:

LIMIT子句可以采用两个参数,它们将提供一个偏移量:

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15 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:

SELECT * FROM tbl LIMIT 95,18446744073709551615; With one argument, the value specifies the number of rows to return from the beginning of the result set:

SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows

LIMIT 子句可用于限制 SELECT 语句返回的行数。LIMIT 接受一个或两个数字参数,它们都必须是非负整数常量(使用准备好的语句时除外)。

有两个参数,第一个参数指定要返回的第一行的偏移量,第二个参数指定要返回的最大行数。初始行的偏移量为 0(不是 1):

SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15 要检索从某个偏移量到结果集末尾的所有行,您可以为第二个参数使用一些大数字。此语句检索从第 96 行到最后一行的所有行:

SELECT * FROM tbl LIMIT 95,18446744073709551615; 使用一个参数,该值指定从结果集的开头返回的行数:

SELECT * FROM tbl LIMIT 5; # 检索前 5 行

http://dev.mysql.com/doc/refman/5.0/en/select.html

http://dev.mysql.com/doc/refman/5.0/en/select.html

So with this:

所以有了这个:

SELECT name, cost FROM test orderby name asc LIMIT 990, 10;

回答by Carson Reinke

SELECT name, cost
FROM (SELECT name, cost FROM test orderby name desc LIMIT 10) as test
ORDER BY name asc;

回答by Jay Ponkia

You can use ROW_NUMBER() clause for this, this will be helpful for you

您可以为此使用 ROW_NUMBER() 子句,这将对您有所帮助

select top"N" * from (select ROW_NUMBER() OVER(Order by Col_Name desc) as RowNo,* from Table_Name) Table_Name

select top"N" * from (select ROW_NUMBER() OVER(Order by Col_Name desc) as RowNo,* from Table_Name) Table_Name