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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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...