mysql SQL statement to copy the contents of a field from one table to a field from another table
- 2020-06-15 10:23:09
- OfStack
Requirement: Copy the contents of a field in one table to a field in another table.
sql statement 1:
UPDATE file_manager_folder f1
LEFT OUTER JOIN file_manager_folder f2
ON f1.name = f2.name AND f2.parentId = 54
SET f1.parentId = 54
WHERE f2.name IS NULL AND f1.id IN (1,2,3);
Implement sql statement 2:
update B set extra = A.extra from A join B on (A.id = B.id);
Implement sql statement 3:
update b set b.sms = (select a.sms from a where a.id = b.id)
You need to make sure that id in both tables is the primary key or the only one
sql statement 4:
UPDATE A SET A.SMS = (SELECT B.SMS FROM B WHERE A.ID = B.ID) WHERE EXISTS (SELECT 1 FROM B WHERE A.ID = B.ID);
Realize sql statement 5:
Copy the data from one table field to the field of another table by writing:
sql statement 5:
UPDATE tb_1 INNER JOIN tb_2 ON tb_1.tid = tb_2.tid
SET tb_1.tcontent = tb_2.tcontent
Attached: same as table copy
Requirement: Copy the contents of one field from the same table to another field
Case 1:
I want to copy the contents of the A field in the article table to the B field in the article table. The sql statement is:
update article set B=A;
Example 2:
Sometimes we need to copy an entire column of data from one field to a new field. This can be as simple as SQL writing:
UPDATE tb_1 SET content_target = content_source;
Roughly written as follows:
Update {your_table} set {source_field} = {object_field} WHERE cause