MySQL from a Table update field to another table _mysql

Source: Internet
Author: User

Let's start with a few simple examples

Solution 1:1 Columns

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.name
where 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 more.

For example, to update the field of a table Tk_zyt_scenery_order to T_advs_order, you might generally write this:

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

This efficiency is relatively low, optimized writing:

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.