在MySQL中复制行 [英] Copying rows in MySQL
问题描述
我想复制一行的所有列,但不必指定每一列.我知道 http://dev.mysql上的语法. com/doc/refman/5.1/en/insert-select.html ,但我看不到忽略列的方法.
I want to copy all of the columns of a row, but not have to specify every column. I am aware of the syntax at http://dev.mysql.com/doc/refman/5.1/en/insert-select.html but I see no way to ignore a column.
在我的示例中,我试图将行的所有列复制到新行中,但主键除外.
For my example, I am trying to copy all the columns of a row to a new row, except for the primary key.
有没有一种方法可以不必在查询中写每个字段?
Is there a way to do that without having to write the query with every field in it?
推荐答案
如果您的id
或主键列是auto_increment
,则可以使用临时表:
If your id
or primary key column is an auto_increment
you can use a temp table:
CREATE TEMPORARY TABLE temp_table
AS
SELECT * FROM source_table WHERE id='7';
UPDATE temp_table SET id='100' WHERE id='7';
INSERT INTO source_table SELECT * FROM temp_table;
DROP TEMPORARY TABLE temp_table;
因此,您可以通过这种方式复制行id ='7'中的所有数据,然后进行分配 新值"100"(或任何值超出了source_table中当前auto_increment值的范围).
so in this way you can copy all data in row id='7' and then assign new value '100' (or whatever value falls above the range of your current auto_increment value in source_table).
注意;声明之后:)
这篇关于在MySQL中复制行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!