10

基本上我有一个版本产品的表,

所以它有两列感兴趣,id | product_id

id是一个autoincrement列,
product_id只是一个int

首次创建产品时,product_id来自id,编辑产品时,我们复制行,因此product_id相同,但 id 不同。所以当我们第一次创建产品时,我们会做两个查询,

插入,然后update table whatever set product_id = id where id = {the insert id}

这行得通,但我想知道是否有办法在一个查询中做到这一点?

请注意,我们只能访问插入、更新、删除。没有触发器或存储过程。

4

2 回答 2

2

使用LAST_INSERT_ID()功能:

update table whatever set
product_id = id
where id = last_insert_id()
于 2012-11-26T11:11:23.170 回答
0

这是单个查询:

insert into whatever 
set product_id = last_insert_id() + 1;
于 2014-08-13T18:39:08.520 回答