简体   繁体   English

选择MySQL中的最后一行

[英]Select last row in MySQL

How can I SELECT the last row in a MySQL table? 如何在MySQL表中SELECT最后一行?

I'm INSERT ing data and I need to retrieve a column value from the previous row. 我正在INSERT数据,我需要从上一行中检索列值。

There's an auto_increment in the table. 表格中有一个auto_increment

Yes, there's an auto_increment in there 是的,那里有一个auto_increment

If you want the last of all the rows in the table, then this is finally the time where MAX(id) is the right answer! 如果您想要表中所有行的最后一行 ,那么最后就是使用MAX(id)正确答案的时候了! Kind of: 的种类:

SELECT fields FROM table ORDER BY id DESC LIMIT 1;

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. 看看我们如何首先按your_auto_increment_field (或您调用的任何名称)以降序对一组无序行进行排序。 Then we limit the resultset to just the first row with LIMIT 1 . 然后,我们将结果集限制为LIMIT 1到第一行。

on tables with many rows are two queries probably faster... 在具有很多行的表上,两个查询可能更快

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

SELECT * FROM table WHERE id = @last_id;

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慢了几分之一秒。

Make it simply use: PDO::lastInsertId 使其简单使用: PDO::lastInsertId

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

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. 如果要添加最近的时间戳,请添加一个时间戳,然后选择按最高时间戳(限制1)的相反顺序排列。如果要按ID排序,请按ID排序。 If you want to use the one you JUST added, use mysql_insert_id . 如果要使用您刚添加的那个,请使用mysql_insert_id

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

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

You can use an OFFSET in a LIMIT command: 您可以在LIMIT命令中使用OFFSET

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返回最后一行

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 is the primary key, there is no performance penalty for using order by versus select max(id) . 附带说明一下,如果您有这样的字段并且它主键,那么使用order byselect 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 1 to be the same as reach the max(id) 主键是如何对数据库文件中的数据进行排序(至少对于InnoDB),并且RDBMS知道数据的结束位置,并且可以order by id + limit 1来优化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. 也许主键是一个自然键,它是3个字段的组合...但是,并不是全部都丢失了。 From a programming language you can first get the number of rows with 从编程语言中,您可以首先获取行数

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

and then use the obtained number in the LIMIT clause 然后在LIMIT子句中使用获得的数字

SELECT * FROM orderbook2
LIMIT <number_from_rowcount>, 1

Unfortunately, MySQL will not allow for a sub-query, or user variable in the LIMIT clause 不幸的是,MySQL在LIMIT子句中不允许子查询或用户变量

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM