基本上我有一个版本产品的表,
所以它有两列感兴趣,id | product_id
id
是一个自增
列,
product_id
只是一个 int
首次创建产品时,product_id
来自 id
,
编辑产品时,我们复制该行,因此 product_id
相同,但 id 不同。所以当我们第一次创建产品时,我们做了两个查询,
插入,然后更新表 whatever set product_id = id where id = {the insert id}
这行得通,但我想知道是否有一种方法可以在一次查询中完成?
请注意,我们只能访问插入、更新和删除。没有触发器或存储过程。
最佳答案
使用 LAST_INSERT_ID()
功能:
update table whatever set
product_id = id
where id = last_insert_id()
关于mysql - 将自动增量值复制到插入时的另一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13563419/