从另一个 table 插入记录,自动递增列新 table

insert record from another table with auto increment column new table

我必须从具有 auto_increment id 字段的 table 中插入记录。

CREATE TEMPORARY TABLE tmptable SELECT * 
FROM  `wp_rg_lead_detail` 
WHERE lead_id =59;
UPDATE tmptable SET lead_id =60 WHERE lead_id =59;
insert into `wp_rg_lead_detail` (`id`, `lead_id`, `form_id`, `field_number`, `value`)
    select `id`, `lead_id`, `form_id`, `field_number`, `value`
    from `tmptable`
    WHERE lead_id =60;

Duplicate entry '1' for key 'PRIMARY'

我需要 auto_increment 在目标 table 中提交 id 应该按上述查询递增。

只是不要插入ID,让列的自动增量属性处理它:

INSERT INTO `wp_rg_lead_detail` 
(`lead_id`, `form_id`, `field_number`, `value`) -- No Id!
SELECT `lead_id`, `form_id`, `field_number`, `value`
FROM   `tmptable`
WHERE  `lead_id` = 60;