MySQL Two association tables how to update data for one table

Source: Internet
Author: User

Today encountered a customer data update problem, two related tables, a main table is used to hold the main information of the document, a sub-table is used to save the document details; Now you want to update the data from one of the fields in the main table to a field in the secondary table.
Hypothesis: A table is the main table, there is a number order_id, open single operator, open a single date Oper_date, Memo memo, etc. b is a sub-table, there is a ticket order_id, serial ID, product code code, name name, Memo memo and so on. The note of a table has data, B table notes no data, now to update the table a data to B table, and B table has data can not be updated. A and B tables are linked by a ticket number. The SQL syntax for updating data is as follows:

Update B,a Set B.memo=a.memo
where a.order_id=b.order_id and (B.memo is null or b.memo= ");

Create TableA (order_idint  not NULLauto_increment, operatorvarchar( -), Oper_date date, Memovarchar( -),    Primary Key(order_id));Create TableB (order_idint  not NULLauto_increment, good_idint, Good_codeint, Good_namevarchar( -), Memovarchar( -),    Primary Key(order_id));Insert  intoAValues (1,'Onion2', now (),'Test 1'),(2,'Onion2', now (),'Test 2'),(3,'Onion3', now (),'Test 3');Insert  intoBValues(1,1,0001,'good1','already notes');Insert  intoB (Order_id,good_id,good_code,good_name)Values (2,2,0002,'good2'),(3,3,0003,'good3');UpdateB,aSetB.memo=A.memowherea.order_id=b.order_id and(B.memo is NULL orB.memo="');

MySQL Two association tables how to update data for one table

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.