MySQL - 选择最后插入的行的最简单方法

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

MySQL - Select the last inserted row easiest way

mysqlselect

提问by Thomas

I simply need to select the last entered row specified by condition, e.g:

我只需要选择由条件指定的最后输入的行,例如:

SELECT ID from bugs WHERE user=Me

I need to return only the very last ID entered by user 'Me'. Is there a simple way to do this? Thank you.

我只需要返回用户“我”输入的最后一个 ID。有没有一种简单的方法可以做到这一点?谢谢你。

回答by Matt

It would be best to have a TIMESTAMPcolumn that defaults to CURRENT_TIMESTAMP.. it is the only true predictive behavior you can find here.

最好有一个TIMESTAMP默认为CURRENT_TIMESTAMP..的列,这是您可以在这里找到的唯一真正的预测行为。

The second-best thing you can do is ORDER BY ID DESC LIMIT 1and hope the newest ID is the largest value.

您可以做的第二好的事情是ORDER BY ID DESC LIMIT 1并希望最新的 ID 是最大的值。

回答by pmrotule

You can use ORDER BY ID DESC, but it's WAY faster if you go that way:

您可以使用ORDER BY ID DESC,但如果您这样做,速度会更快:

SELECT * FROM bugs WHERE ID = (SELECT MAX(ID) FROM bugs WHERE user = 'me')

In case that you have a huge table, it could make a significant difference.

如果您有一张大桌子,它可能会产生重大影响。

EDIT

编辑

You can even set a variable in case you need it more than once (or if you think it is easier to read).

您甚至可以设置一个变量,以防您多次需要它(或者如果您认为它更易于阅读)。

SELECT @bug_id := MAX(ID) FROM bugs WHERE user = 'me';
SELECT * FROM bugs WHERE ID = @bug_id;

回答by SiLent SoNG

SELECT MAX(ID) from bugs WHERE user=Me

回答by Thomas

One way to accomplish that is to order you records and limit to 1. For example if you have the following table ('data').

实现这一点的一种方法是对您的记录进行排序并限制为 1。例如,如果您有下表(“数据”)。

    id | user | price
   -------------------
    1  |  me  | 40.23
    2  |  me  | 10.23

Try the following sql query

尝试以下 sql 查询

  select * from data where user='me' order by id desc limit 1

回答by Wayne

In concurrency, the latest record may not be the record you just entered. It may better to get the latest record using the primary key.

在并发中,最新的记录可能不是您刚刚输入的记录。使用主键获取最新记录可能会更好。

If it is a auto increment field, use SELECT LAST_INSERT_ID();to get the id you just created.

如果是自增字段,则使用SELECT LAST_INSERT_ID();来获取您刚刚创建的 id。

回答by rommo roy

SELECT * FROM `table_name` 
ORDER BY `table_name`.`column_name` DESC
LIMIT 1 

回答by pratik shourabh

Just after running mysql query from php

就在从 php 运行 mysql 查询之后

get it by

得到它

$lastid=mysql_insert_id();

this give you the alst auto increment id value

这给你最后的自动增量 id 值

回答by Kunwar Maurya

SELECT ID from bugs WHERE user=Me ORDER BY CREATED_STAMP DESC; BY CREATED_STAMP DESC fetches those data at index first which last created.

SELECT ID from bug WHERE user=Me ORDER BY CREATED_STAMP DESC; BY CREATED_STAMP DESC 首先在最后创建的索引处获取那些数据。

I hope it will resolve your problem

我希望它能解决你的问题