MySQL ON DUPLICATE KEY - 最后插入ID?
我有以下查询:
INSERT INTO table (a) VALUES (0)
ON DUPLICATE KEY UPDATE a=1
我想要插入或更新的 ID。 通常我会运行第二个查询来获取此信息,因为我相信 insert_id() 只返回“插入”ID 而不是更新后的 ID。
有没有办法在不运行两个查询的情况下插入/更新并检索行的 ID?
I have the following query:
INSERT INTO table (a) VALUES (0)
ON DUPLICATE KEY UPDATE a=1
I want the ID of either the insert or the update. Usually I run a second query in order to get this as I believe insert_id() only returns the 'inserted' ID and not the updated ID.
Is there a way to INSERT/UPDATE and retrieve the ID of the row without running two queries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
查看此页面:https://web.archive。 org/web/20150329004325/https://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
在页面底部,他们解释了如何通过将表达式传递给该 MySQL 函数来使 LAST_INSERT_ID 对更新有意义。
来自 MySQL 文档示例:
Check this page out: https://web.archive.org/web/20150329004325/https://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
At the bottom of the page they explain how you can make LAST_INSERT_ID meaningful for updates by passing an expression to that MySQL function.
From the MySQL documentation example:
确切地说,如果这是原始查询:
并且“id”是自动递增主键,那么这将是可行的解决方案:
全部都在这里: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
To be exact, if this is the original query:
and 'id' is the auto-increment primary key than this would be the working solution:
Is all here: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
您可能会查看 REPLACE,如果记录存在,它本质上是删除/插入。 但这会更改自动增量字段(如果存在),这可能会破坏与其他数据的关系。
You might look at REPLACE, which is essentially a delete/insert if the record exists. But this would change the auto increment field if present, which could break relationships with other data.
我不知道你的 MySQL 版本是什么,但是对于 InnoDB,5.1.20 中存在 autoinc bug,
并在 5.1.23 中更正
http://bugs.mysql.com/bug.php?id=27405
5.1.31 中的错误并在 5.1.33 中更正
http://bugs.mysql.com/bug.php?id=42714
I don't know what is your version of MySQL but with InnoDB, there was bug with autoinc
bug in 5.1.20 and corrected in 5.1.23
http://bugs.mysql.com/bug.php?id=27405
bug in 5.1.31 and corrected in 5.1.33
http://bugs.mysql.com/bug.php?id=42714
我遇到一个问题,当 ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id) 时主键增加 1。因此会话中下一个输入的 id 将增加 2
I have come across a problem, when ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id) increment the primary key by 1. So the id of the next input within the session will be incremented by 2
值得注意的是,这可能是显而易见的(但为了清楚起见,我无论如何都会在这里说),REPLACE 将在插入新数据之前清除现有的匹配行。 ON DUPLICATE KEY UPDATE 将仅更新您指定的列并保留该行。
来自手册:
It's worth noting, and this might be obvious (but I'll say it anyway for clarity here), that REPLACE will blow away the existing matching row before inserting your new data. ON DUPLICATE KEY UPDATE will only update the columns you specify and preserves the row.
From the manual:
如果您使用自动增量,现有的解决方案就可以工作。 我遇到的情况是,用户可以定义一个前缀,并且应该在 3000 处重新启动序列。由于前缀不同,我无法使用自动增量,这使得用于插入的 last_insert_id 为空。 我用以下方法解决了这个问题:
如果前缀存在,它将增加它并填充last_insert_id。
如果前缀不存在,它将插入值为 3000 的前缀,并用 3000 填充last_insert_id。
The existing solutions work if you use autoincrement. I have a situation where the user can define a prefix and it should restart the sequence at 3000. Because of this varied prefix, I cannot use autoincrement, which makes last_insert_id empty for inserts. I solved it with the following:
If the prefix exists, it will increment it and populate last_insert_id.
If the prefix does not exist, it will insert the prefix with the value 3000 and populate last_insert_id with 3000.