insert into table select * from table where primarykey=1
I just want to copy one row to insert into the same table (i.e., I want to duplicate an ex
max233 was certainly on the right track, at least for the autoincrement case. However, do not do the ALTER TABLE. Simply set the auto-increment field in the temporary table to NULL. This will present an error, but the following INSERT of all fields in the temporary table will happen and the NULL auto field will obtain a unique value.
If the Primary Key is Auto Increment, just specify each field except the primary key.
INSERT INTO table(field1,field2,field3)
SELECT (field1,field2,field3) FROM table
WHERE primarykey=1
I'm assuming you want the new record to have a new primarykey
? If primarykey
is AUTO_INCREMENT
then just do this:
INSERT INTO table (col1, col2, col3, ...)
SELECT col1, col2, col3, ... FROM table
WHERE primarykey = 1
...where col1, col2, col3, ...
is all of the columns in the table except for primarykey
.
If it's not an AUTO_INCREMENT
column and you want to be able to choose the new value for primarykey
it's similar:
INSERT INTO table (primarykey, col2, col3, ...)
SELECT 567, col2, col3, ... FROM table
WHERE primarykey = 1
...where 567
is the new value for primarykey
.
You could also try dumping the table, finding the insert command and editing it:
mysqldump -umyuser -p mydatabase --skip-extended-insert mytable > outfile.sql
The --skip-extended-insert
gives you one insert command per row. You may then find the row in your favourite text editor, extract the command and alter the primary key to "default".
Some of the following was gleaned off of this site. This is what I did to duplicate a record in a table with any number of fields:
This also assumes you have an AI field at the beginning of the table
function duplicateRow( $id = 1 ){
dbLink();//my db connection
$qColumnNames = mysql_query("SHOW COLUMNS FROM table") or die("mysql error");
$numColumns = mysql_num_rows($qColumnNames);
for ($x = 0;$x < $numColumns;$x++){
$colname[] = mysql_fetch_row($qColumnNames);
}
$sql = "SELECT * FROM table WHERE tableId = '$id'";
$row = mysql_fetch_row(mysql_query($sql));
$sql = "INSERT INTO table SET ";
for($i=1;$i<count($colname)-4;$i++){//i set to 1 to preclude the id field
//we set count($colname)-4 to avoid the last 4 fields (good for our implementation)
$sql .= "`".$colname[$i][0]."` = '".$row[$i]. "', ";
}
$sql .= " CreateTime = NOW()";// we need the new record to have a new timestamp
mysql_query($sql);
$sql = "SELECT MAX(tableId) FROM table";
$res = mysql_query($sql);
$row = mysql_fetch_row($res);
return $row[0];//gives the new ID from auto incrementing
}
If your table's primary key field is an auto increment field, then you can use query with columns. For example, your table named test_tbl
has 3 fields as id, name, age
. id
is a primary key field and auto increment, so you can use the following query to duplicate the row:
INSERT INTO `test_tbl` (`name`,`age`) SELECT `name`,`age` FROM `test_tbl`;
This query results in duplicating every row.
If your table's primary key field is not an auto increment field, then you can use the following method:
INSERT INTO `test_tbl` (`id`,`name`,`age`)
SELECT 20,`name`,`age` FROM `test_tbl` WHERE id = 19;
The result of this query is a duplicate row of id=19
inserted as id=20
.