在 MySQL 中,我试图将具有 autoincrement column ID=1
的行和 insert 数据复制到同一个表中作为新的column ID=2
的行。
如何在单个查询中执行此操作?
最佳答案
使用 INSERT ... SELECT
:
insert into your_table (c1, c2, ...)
select c1, c2, ...
from your_table
where id = 1
其中c1, c2, ...
是除id
之外的所有列。如果您想使用 2 的 id
显式插入,则将其包含在 INSERT 列列表和 SELECT 中:
insert into your_table (id, c1, c2, ...)
select 2, c1, c2, ...
from your_table
where id = 1
当然,在第二种情况下,您必须注意可能重复的 id
为 2。
关于mysql - 如何在 MySQL 中使用自动增量字段复制行并插入到同一个表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9156340/