'cut rows from one table and paste them into another table [closed]
I want to cut rows from one table and paste them into another table. is there a mysql command that does this job ?
table1(id, field1, field2, fields3 )
table2(id, field1, field2, fields3 )
Solution 1:[1]
Ok, let's assume we need to copy a row from TableOld to TableNew.
First we will copy the row from TableOld to TableNew.
INSERT INTO TableNew
SELECT * FROM TableOld
WHERE [Conditions]
Now we will delete the row from TableOld.
DELETE * FROM TableOld
WHERE [Conditions]
Solution 2:[2]
There is no 'cut - paste' command in mysql, however you can start a transaction and insert into one table then delete from the other one.
Lets say you have the following 2 tables
create table cut (
ID INT NOT NULL AUTO_INCREMENT,
Field1 INT NOT NULL,
PRIMARY KEY(ID)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
create table paste (
ID INT NOT NULL AUTO_INCREMENT,
Field1 INT NOT NULL,
PRIMARY KEY(ID)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
You want to insert some test data:
insert cut set Field1="1";
Then, you need to 'copy' from one table to another one using insert and select. At last, you want to remove from 'cut' table as follows
start transaction;
insert into paste (Field1) select Field1 from cut where id=1;
delete from cut where id=1;
commit;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | anantdark |
| Solution 2 | Nick Arnie |
