I want to copy several rows in a mysql table based on a foreign key, and asign the new rows a new foreign key ID. Assuming my table layout looks like this:
test
-----
table1_id int(11)
value varchar(20)
How do I accomplish this?
I want to copy several rows in a mysql table based on a foreign key, and asign the new rows a new foreign key ID. Assuming my table layout looks like this:
test
-----
table1_id int(11)
value varchar(20)
How do I accomplish this?
Found out that the query would need to look like this:INSERT INTO test (table1_id, value) (SELECT '2', value FROM test WHERE table1_id=1)
which would copy all rows with the foreign key ID '1' and assign the new rows with ID 2 instead. If the table contains more rows you could add them or change order in the SELECT part, like this
...(SELECT row1, '{$new_id}', value, another_row FROM...