大家好,我有以下查询,效果很好,但我知道需要将插入表中生成的唯一 ID 添加到目标表
insert into claims.third_party(tp_names,tp_insurer,tp_registration)
select c.tpnames,c.tpinsurers,c.tpregistration from claims as c;
换句话说,我需要返回唯一的 id 并将其添加到源表中,创建外键链接
最佳答案
您可以使用 LAST_INSERT_ID() 获取最后一个自动增量值。
选择 LAST_INSERT_ID();
http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id
更新:
将 ROW_COUNT() 与 LAST_INSERT_ID() 一起使用,您将生成生成的键范围。
[LAST_INSERT_ID(), (LAST_INSERT_ID() + ROW_COUNT())]
关于mysql - 使用mysql插入时更新源表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21890487/