I have a table in the database called user, with n fields, how can I duplicate a certain record only with a new id?
insert into usuarios * select * from usuarios where id = x?
obs: this id is a non-null primary key
I have a table in the database called user, with n fields, how can I duplicate a certain record only with a new id?
insert into usuarios * select * from usuarios where id = x?
obs: this id is a non-null primary key
You can make a INSERT
SELECT
at the same time, like this:
INSERT INTO tabela (campo1, caompo2, ...)
SELECT campo1, caompo2, ... FROM tabela WHERE primary_key = 3;
Note:
insert
and select
. null
. If you want to copy the line without specifying all the fields, you can do this:
Create a temporary table and associate the line you want to copy
CREATE TEMPORARY TABLE tmp SELECT * FROM tabela WHERE id = 3;
Update the temp
table with a new id
UPDATE tmp SET id_conta = 4 WHERE id_conta = 3;
Now, just apply INSERT
INSERT INTO conta SELECT * FROM tmp WHERE id_conta = 4;