如何获取MYSQL中最后更新记录的主键? [英] How to get the primary key of last updated record in MYSQL?

查看:420
本文介绍了如何获取MYSQL中最后更新记录的主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道,如果主键是autocrement,SELECT语句可以返回我需要的。
UPDATE?



使用last_insert_id是线程安全的?

解决方案

使用last_insert_id是线程安全的

安全,这可能是你的意思。从文档: / p>


生成的ID是
,保存在服务器上的
每个连接的基础。这意味着
函数返回给
a给定的客户端的值是第一个为
生成的
AUTO_INCREMENT 值语句影响该客户端的
AUTO_INCREMENT
此值不能受其他
客户端影响,即使它们生成自己的
AUTO_INCREMENT 值。
此行为确保每个客户端
可以检索其自己的ID,而不需要
关注其他
客户端的活动,并且不需要
锁或事务。 / p>

(他们的重点。)


We know if the primary key is autocrement, SELECT statement could return what I need. What about UPDATE?

Using last_insert_id is thread safe?

解决方案

Using last_insert_id is thread safe?

It's connection-safe, which is probably what you mean. From the docs:

The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.

(Their emphasis.)

这篇关于如何获取MYSQL中最后更新记录的主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆