MySQL from one table update field to another table _ MySQL

Source: Internet
Author: User
With MySQL or higher, you can add two or more tables to one table. by adding two tables together, you can update the records of one table in the relevant fields and set them to another table. Let's take a few simple examples.

Solution 1: 1 column

update student s, city c  set s.city_name = c.name where s.city_code = c.code;

Solution 2: multiple columns

update a, b set a.title=b.title, a.name=b.namewhere a.id=b.id

Solution 3: subquery

update student s set city_name = (select name from city where code = s.city_code);

Let's take a look at a few

For example, if you update the field tk_zyt_scenery_order in the table to t_advs_order, it is generally written as follows:

UPDATE t_advs_order SET  attribute1=(SELECT o.order_state FROM tk_zyt_scenery_order o WHERE o.order_id=`on`),  attribute2=(SELECT o.order_state FROM tk_zyt_scenery_order o WHERE o.order_id=`on`)  WHERE EXISTS (SELECT o.order_state FROM tk_zyt_scenery_order o WHERE o.order_id=`on`);

In this way, the efficiency is relatively low and the writing method is optimized:

UPDATE t_advs_order a INNER JOIN tk_zyt_scenery_order s ON s.order_id=a.`on` SET  a.attribute1=s.order_id,  a.attribute2=s.order_id;

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.