我正在努力做到这一点
我正在这样尝试:
CREATE TEMPORARY TABLE tmptable SELECT * FROM blogs WHERE lan = 2;
UPDATE tmptable SET lan = 1;
INSERT INTO blogs SELECT * FROM tmptable; dump database tmptable;
但是,我会得到重复的 key 错误...
我该如何预防?
-编辑-
我试过了:
CREATE TEMPORARY TABLE tmptable SELECT * FROM blogs WHERE lan = 2;
UPDATE tmptable SET lan = 1;
ALTER TABLE tmptable DROP id;
INSERT INTO blogs SELECT * FROM tmptable; dump database tmptable;
但是然后
Column count doesn't match value count at row 1
-编辑-
我相信这会奏效(确实如此,因为我知道有多少条记录)
CREATE TEMPORARY TABLE tmptable SELECT * FROM blogs WHERE lan = 2;
UPDATE tmptable SET lan = 1;
UPDATE tmptable SET id = id + 1000;
INSERT INTO blogs SELECT * FROM tmptable;
但是我该怎么做呢? (只需为主键(id)设置下一个可用的自动增量值(不使用PHP/类似方法))
-编辑-
也许是这样的???
CREATE TEMPORARY TABLE tmptable SELECT * FROM blogs WHERE lan = 2;
UPDATE tmptable SET lan = 1;
UPDATE tmptable SET id = id + (SELECT id FROM blogs ORDER BY id DESC LIMIT 1);
INSERT INTO blogs SELECT * FROM tmptable;
最佳答案
CREATE TEMPORARY TABLE tmptable SELECT * FROM blogs WHERE lan = 2;
UPDATE tmptable SET lan = 1;
alter table tmptable drop column id;
INSERT INTO blogs SELECT NULL,tmptable.* FROM tmptable;
假定列“id”是第一个列。
关于mysql - 复制表中的所有行并防止重复的键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16468850/