Home >Database >Mysql Tutorial >How to Efficiently Duplicate Rows in MySQL with Many Columns?
Problem:
Copying a row into the same table can be a challenge when dealing with a large number of columns. The traditional method involves explicitly listing all columns in the SELECT statement, but this becomes impractical with extensive tables.
Proposed Solution:
Using a temporary table as an intermediary can circumvent the issue of specifying each column. However, this involves creating a temporary table with identical columns and executing multiple statements for the duplication process.
Leonard Challis's Technique:
Leonard Challis offers an alternative approach that simplifies the process:
<br>CREATE TEMPORARY TABLE tmptable_1 SELECT * FROM table WHERE primarykey = 1;<br>UPDATE tmptable_1 SET primarykey = NULL;<br>INSERT INTO table SELECT * FROM tmptable_1;<br>DROP TEMPORARY TABLE IF EXISTS tmptable_1;<br>
Advantages:
Additional Considerations:
Conclusion:
Leonard Challis's technique provides a straightforward and efficient solution for duplicating rows in tables with numerous columns. It avoids the need to manually list columns, reducing potential errors and simplifying the process.
The above is the detailed content of How to Efficiently Duplicate Rows in MySQL with Many Columns?. For more information, please follow other related articles on the PHP Chinese website!