MySQL current_insert_id()? (不是last_insert_id())

发布于 2024-07-20 18:21:47 字数 210 浏览 7 评论 0原文

我正在插入一行,其中包含一个 char 列,用于基于(除其他外)行的自动 id 的哈希值。

我知道我可以插入它,获取 insert_id,计算哈希值,然后更新它。

有谁知道在单个查询中执行此操作的方法? 您在插入时需要行 insert_id 。 这是完全不可能的,还是有类似 current_insert_id() 的东西......

谢谢!

I am inserting a row with a char column for a hash based on (among other things) the row's auto id.

I know I can insert it, fetch the insert_id, calculate the hash, and update it.

Does anyone know of a way to do this in a single query? You would need the rows insert_id at the time of insert. Is that completely impossible, or is there something like current_insert_id()...

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

甜尕妞 2024-07-27 18:21:47

不,MySQL 中没有函数可以为您提供current_insert_id()

从 MySQL 中的 AUTO_INCRMENT 字段获取生成的 ID 值的唯一方法是执行 INSERT,然后调用 last_insert_id()。 因此,您可能需要执行单独的 UPDATE 来计算哈希值的计划。

我可以想到另外两个替代方案:

  • 在 INSERT 之前使用除 AUTO_INCRMENT 之外的其他机制自行生成唯一值。 例如,请参阅 UUID()函数。

    SET @id = SELECT UUID(); 
      INSERT INTO MyTable (id, hash) VALUES (@id, hash(@id...)); 
      

  • 不要在哈希计算中包含 ID。

No, there's no function in MySQL that gives you the current_insert_id().

The only way to get a generated ID value from an AUTO_INCREMENT field in MySQL is to do the INSERT and then call last_insert_id(). So your plan of doing a separate UPDATE to calculate the hash is probably what you'll have to do.

I can think of two other alternatives:

  • Generate the unique value yourself before the INSERT with some other mechanism besides the AUTO_INCREMENT. For example, see the UUID() function.

    SET @id = SELECT UUID();
    INSERT INTO MyTable (id, hash) VALUES (@id, hash(@id...));
    
  • Don't include the ID in your hash calculation.

画▽骨i 2024-07-27 18:21:47

据我所知,没有办法在 MySQL 中通过一个查询来完成此操作,但您可以在您选择的服务器端脚本语言中执行类似的操作:

<?php

$query = mysql_query("SHOW TABLE STATUS LIKE 'MyTable'");
$row = mysql_fetch_assoc($query);
$next_id = $row['Auto_increment'];

?>

...这为您提供了要合并到 SQL 中的 id。

编辑:我还找到了这个答案< /a> 这可能会有所帮助。

There's no way that I know of to do it in MySQL in one query, but you could do something like this in your server-side scripting language of choice:

<?php

$query = mysql_query("SHOW TABLE STATUS LIKE 'MyTable'");
$row = mysql_fetch_assoc($query);
$next_id = $row['Auto_increment'];

?>

...which gives you the id to incorporate in your SQL.

EDIT: I also found this answer which may be helpful.

忘羡 2024-07-27 18:21:47

您可以从 information_schema.TABLES 表中查询下一个要使用的值,其中的 AUTO_INCRMENT 列。 (你可能正在为自己设置竞争条件?)

You can query the next-to-be-used value from the information_schema.TABLES table, the AUTO_INCREMENT column there. (You might be setting yourself up for a race condition?)

我家小可爱 2024-07-27 18:21:47

当我进行插入时,我会执行如下操作:

INSERT INTO table (col1,col2) VALUES (data1,data2);SELECT LAST_INSERT_ID()

并像获取数据一样运行查询。 在 VB.NET 中,语法是(假设您有 MySql.Data.MySqlClient .dll):

Dim sql As String = "[sql string above]"
Dim dr As MySqlDataReader = YourRetrieveDataFunction(sql)

dr.Read()
yourObjectInstance.ID = dr(0)
dr.Close

从技术上讲,它是两个查询,但仅对数据库进行一次命中:)

When I do inserts I do something like this:

INSERT INTO table (col1,col2) VALUES (data1,data2);SELECT LAST_INSERT_ID()

and just run the query like I was fetching data. In VB.NET the syntax is (assuming you have the MySql.Data.MySqlClient .dll):

Dim sql As String = "[sql string above]"
Dim dr As MySqlDataReader = YourRetrieveDataFunction(sql)

dr.Read()
yourObjectInstance.ID = dr(0)
dr.Close

It's technically two queries, but only one hit on the database :)

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