Mysql _mysql The SQL statement that copies the contents of one of a table's fields to a field in another table.

Source: Internet
Author: User
Tags one table


Requirements: Copy one of the field contents of a table to a field in another table.

Implementing SQL Statement 1:

Copy Code code as follows:

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);

Implementing SQL Statement 2:

Copy Code code as follows:
Update B Set extra = A.extra from join B on (a.id = b.id);

Implementing SQL Statement 3:

Copy Code code as follows:
Update B Set b.sms = (select A.sms from a WHERE a.id = b.ID)

You need to determine that the IDs in the two tables are primary keys or unique

Implementing SQL Statement 4:

Copy Code code as follows:

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);


Implementing SQL Statement 5:
To copy one table field data to another, you can write this:
Implementing SQL Statement 5:
Copy Code code as follows:

UPDATE tb_1 INNER JOIN tb_2 on tb_1.tid = Tb_2.tid
SET tb_1.tcontent = tb_2.tcontent


Attached: copy of same table

Requirements: Copy the contents of one field from the same table to another field

Example 1:
I want to copy the contents of a field from the article table to the B field in the article table. The SQL statement is:

Copy Code code as follows:
Update article set b=a;


Example 2:
Sometimes we need to copy a whole column of data from one field to another in a new field, which is simple, SQL can write:
Copy Code code as follows:
UPDATE tb_1 SET content_target = Content_source;

The general wording is as follows:
Copy Code code as follows:
Update {your_table} set {Source_field} = {Object_field} WHERE cause

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.