MySQL Update old data

Source: Internet
Author: User

Scene:

There are two of the following tables

TableA

Id ROW1 ROW2 ROW3 ROW4
1 2 3 4 5

TableB

Id rowa_id Rowa_row4
1 2


At the beginning of the table B only ID, rowa_id two fields, for some convenience reasons, and so on, now to add a field "Rowa_row4", now to the previous records to supplement the corresponding ROWA_ROW4 data;


Analysis:

My first thought was to write a piece of Java code that loops out the data from the TableA and deposits it into the tableb;

Later Raigo gave a simple SQL solution, as follows.


SQL code

--Set not auto commit

SET AUTOTATBT = 0;

--Update data

UPDATE TableB TB

SET TB. Rowa_row4= (

SELECT ROW4 from TableA ta WHERE ta.id= TB. ROWA_ID);


--whether the query achieves the effect

SELECT * from TableA TA, TableB TB where ta.id= TB. rowa_id;


This article is from the "Ydhome" blog, make sure to keep this source http://ydhome.blog.51cto.com/8948432/1875992

MySQL Update old data

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.