insert into table select * from table where primarykey=1
我想复制表中的一行数据并将其插入到同一张表里(即,我想要复制该表中的一个现有行),但我不想在“select”之后列出所有列,因为这个表有太多的列。
但是,当我尝试这样做时,我收到了以下错误:
重复条目'xxx',键为1
我可以通过创建另一个与原表具有相同列的临时容器来复制所需记录以避开这个问题:
create table oldtable_temp like oldtable;
insert into oldtable_temp select * from oldtable where key=1;
update oldtable_tem set key=2;
insert into oldtable select * from oldtable where key=2;
有没有更简单的方法来解决这个问题?
max(oldtable.id) + oldtable_temp.key
的方式,这样我可以确保id递增且唯一。 - guy mograbiupdate oldtable_tem set key=2;
,而是使用update oldtable_tem set key=NULL;
,然后简单地使用insert into oldtable select * from oldtable_tem;
。 - Timofey Bugaevsky