选择 MySQL 中的最后一行

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

Select last row in MySQL

mysql

提问by esqew

How can I SELECTthe last row in a MySQL table?

我怎样才能SELECT在 MySQL 表中的最后一行?

I'm INSERTing data and I need to retrieve a column value from the previous row.

我正在INSERT处理数据,我需要从前一行检索列值。

There's an auto_incrementin the table.

auto_increment桌子上有一个。

回答by Pekka

Yes, there's an auto_increment in there

是的,里面有一个 auto_increment

If you want the last of all the rowsin the table, then this is finally the time where MAX(id)is the right answer! Kind of:

如果您想要表中所有行的最后一行,那么这终于MAX(id)是正确答案的时候了!的种类:

SELECT fields FROM table ORDER BY id DESC LIMIT 1;

回答by Daniel Vassallo

Keep in mind that tables in relational databases are just sets of rows. And sets in mathematics are unordered collections. There is no first or last row; no previous row or next row.

请记住,关系数据库中的表只是行集。数学中的集合是无序的集合。没有第一行或最后一行;没有上一行或下一行。

You'll have to sort your set of unordered rows by some field first, and then you are free the iterate through the resultset in the order you defined.

您必须首先按某个字段对您的一组无序行进行排序,然后您可以按照您定义的顺序自由迭代结果集。

Since you have an auto incrementing field, I assume you want that to be the sorting field. In that case, you may want to do the following:

由于您有一个自动递增字段,我假设您希望它成为排序字段。在这种情况下,您可能需要执行以下操作:

SELECT    *
FROM      your_table
ORDER BY  your_auto_increment_field DESC
LIMIT     1;

See how we're first sorting the set of unordered rows by the your_auto_increment_field(or whatever you have it called) in descending order. Then we limit the resultset to just the first row with LIMIT 1.

看看我们是如何首先按your_auto_increment_field降序排列的(或您调用的任何名称)对一组无序的行进行排序。然后我们将结果集限制在第一行LIMIT 1

回答by spacepille

on tables with many rows are two queries probably faster...

在多行的表上,两个查询可能更快......

SELECT @last_id := MAX(id) FROM table;

SELECT * FROM table WHERE id = @last_id;

回答by vzr

You can combine two queries suggested by @spacepille into single query that looks like this:

您可以将@spacepille 建议的两个查询组合成一个查询,如下所示:

SELECT * FROM `table_name` WHERE id=(SELECT MAX(id) FROM `table_name`);

It should work blazing fast, but on INNODB tables it's fraction of milisecond slower than ORDER+LIMIT.

它应该工作得非常快,但在 INNODB 表上它比 ORDER+LIMIT 慢几毫秒。

回答by macio.Jun

Make it simply use: PDO::lastInsertId

让它简单地使用: PDO::lastInsertId

http://php.net/manual/en/pdo.lastinsertid.php

http://php.net/manual/en/pdo.lastinsertid.php

回答by EboMike

If you want the most recently added one, add a timestamp and select ordered in reverse order by highest timestamp, limit 1. If you want to go by ID, sort by ID. If you want to use the one you JUST added, use mysql_insert_id.

如果要最近添加的,请添加时间戳并选择按最高时间戳倒序排序,限制为 1。如果要按 ID 排序,则按 ID 排序。如果您想使用刚刚添加的那个,请使用mysql_insert_id.

回答by Sani Kamal

Almost every database table, there's an auto_increment column(generally id )

几乎每个数据库表,都有一个 auto_increment 列(一般是 id )

If you want the last of all the rows in the table,

如果你想要表中所有行的最后一个,

SELECT columns FROM table ORDER BY id DESC LIMIT 1;

OR

或者

You can combine two queries into single query that looks like this:

您可以将两个查询合并为一个查询,如下所示:

SELECT columns FROM table WHERE id=(SELECT MAX(id) FROM table);

回答by Ilia Gilmijarow

Many answers here say the same (order by your auto increment), which is OK, provided you have an autoincremented column that is indexed.

这里的许多答案都说相同的(按自动增量排序),这没问题,前提是您有一个已编入索引的自动增量列。

On a side note, if you have such field and it isthe primary key, there is no performance penalty for using order byversus select max(id). The primary key is how data is ordered in the database files (for InnoDB at least), and the RDBMS knows where that data ends, and it can optimize order by id + limit 1to be the same as reach the max(id)

附带说明一下,如果您有这样的字段并且它主键,则使用order byvs不会有性能损失select max(id)。主键是数据在数据库文件中的排序方式(至少对于 InnoDB 而言),RDBMS 知道数据在哪里结束,并且可以优化order by id + limit 1为与到达相同的max(id)

Now the road less traveled is when you don't have an autoincremented primary key. Maybe the primary key is a natural key, which is a composite of 3 fields... Not all is lost, though. From a programming language you can first get the number of rows with

现在少走的路是当您没有自动递增的主键时。也许主键是一个自然键,它是 3 个字段的组合......不过,并不是所有的都丢失了。从编程语言中,您可以首先获得行数

SELECT Count(*) - 1 AS rowcount FROM <yourTable>;

and then use the obtained number in the LIMITclause

然后在LIMIT子句中使用获得的数字

SELECT * FROM orderbook2
LIMIT <number_from_rowcount>, 1

Unfortunately, MySQL will not allow for a sub-query, or user variable in the LIMITclause

不幸的是,MySQL 不允许子查询或LIMIT子句中的用户变量

回答by karthikeyan

SELECT * FROM adds where id=(select max(id) from adds);

This query used to fetch the last record in your table.

此查询用于获取表中的最后一条记录。

回答by Benvorth

You can use an OFFSETin a LIMITcommand:

您可以OFFSETLIMIT命令中使用 an :

SELECT * FROM aTable LIMIT 1 OFFSET 99

in case your table has 100 rows this return the last row without relying on a primary_key

如果你的表有 100 行,这将返回最后一行而不依赖于 primary_key