MySQL ON DUPLICATE KEY - 最后插入ID?

发布于 2024-07-17 02:43:01 字数 227 浏览 5 评论 0原文

我有以下查询:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦明 2024-07-24 02:43:01

查看此页面:https://web.archive。 org/web/20150329004325/https://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
在页面底部,他们解释了如何通过将表达式传递给该 MySQL 函数来使 LAST_INSERT_ID 对更新有意义。

来自 MySQL 文档示例:

如果表包含 AUTO_INCRMENT 列并且 INSERT ... UPDATE 插入行,则 LAST_INSERT_ID() 函数返回 AUTO_INCREMENT 值。 如果该语句改为更新一行,则 LAST_INSERT_ID() 没有意义。 但是,您可以使用 LAST_INSERT_ID(expr) 来解决此问题。 假设 id 是 AUTO_INCREMENT 列。 要使 LAST_INSERT_ID() 对更新有意义,请按如下方式插入行:

INSERT INTO table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;

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:

If a table contains an AUTO_INCREMENT column and INSERT ... UPDATE inserts a row, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value. If the statement updates a row instead, LAST_INSERT_ID() is not meaningful. However, you can work around this by using LAST_INSERT_ID(expr). Suppose that id is the AUTO_INCREMENT column. To make LAST_INSERT_ID() meaningful for updates, insert rows as follows:

INSERT INTO table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;
情绪操控生活 2024-07-24 02:43:01

确切地说,如果这是原始查询:

INSERT INTO table (a) VALUES (0)
 ON DUPLICATE KEY UPDATE a=1

并且“id”是自动递增主键,那么这将是可行的解决方案:

INSERT INTO table (a) VALUES (0)
  ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), a=1

全部都在这里: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

如果表包含 AUTO_INCRMENT 列和 INSERT ... UPDATE
插入一行,LAST_INSERT_ID()函数返回
自动递增值。 如果该语句改为更新一行,
LAST_INSERT_ID() 没有意义。 但是,您可以解决这个问题
通过使用 LAST_INSERT_ID(expr)。 假设 id 是 AUTO_INCRMENT
专栏。

To be exact, if this is the original query:

INSERT INTO table (a) VALUES (0)
 ON DUPLICATE KEY UPDATE a=1

and 'id' is the auto-increment primary key than this would be the working solution:

INSERT INTO table (a) VALUES (0)
  ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), a=1

Is all here: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

If a table contains an AUTO_INCREMENT column and INSERT ... UPDATE
inserts a row, the LAST_INSERT_ID() function returns the
AUTO_INCREMENT value. If the statement updates a row instead,
LAST_INSERT_ID() is not meaningful. However, you can work around this
by using LAST_INSERT_ID(expr). Suppose that id is the AUTO_INCREMENT
column.

走过海棠暮 2024-07-24 02:43:01

您可能会查看 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.

蓝海似她心 2024-07-24 02:43:01

我不知道你的 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

故事与诗 2024-07-24 02:43:01

我遇到一个问题,当 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

下壹個目標 2024-07-24 02:43:01

值得注意的是,这可能是显而易见的(但为了清楚起见,我无论如何都会在这里说),REPLACE 将在插入新数据之前清除现有的匹配行。 ON DUPLICATE KEY UPDATE 将仅更新您指定的列并保留该行。

来自手册

REPLACE 的工作原理与 INSERT 完全相同,
除非表中存在旧行
与 a 的新行具有相同的值
PRIMARY KEY 或 UNIQUE 索引,旧的
行在新行之前被删除
已插入。

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:

REPLACE works exactly like INSERT,
except that if an old row in the table
has the same value as a new row for a
PRIMARY KEY or a UNIQUE index, the old
row is deleted before the new row is
inserted.

紧拥背影 2024-07-24 02:43:01

如果您使用自动增量,现有的解决方案就可以工作。 我遇到的情况是,用户可以定义一个前缀,并且应该在 3000 处重新启动序列。由于前缀不同,我无法使用自动增量,这使得用于插入的 last_insert_id 为空。 我用以下方法解决了这个问题:

INSERT INTO seq_table (prefix, id) VALUES ('$user_prefix', LAST_INSERT_ID(3000)) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id + 1);
SELECT 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:

INSERT INTO seq_table (prefix, id) VALUES ('$user_prefix', LAST_INSERT_ID(3000)) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id + 1);
SELECT LAST_INSERT_ID();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文