MySQL extracts data from one table update another table (fix inconsistent table data)

Source: Internet
Author: User
Tags one table

Currently encountering a data inconsistency, there are two tables, a project table, a project member table, the Project table has a field is the project work time, is the project member's working time summary. It's because of the logic, so change the data to be consistent.

The approximate structure of the project table is as follows.

Table Name: Project

The Project members table is broadly structured as follows.

Table Name: Projectmember

The PID of the Projectmember table is associated with Project ID, and Project_worktime is member_worktime.

Insert three data into Project table.

Projectmember table data is as follows:

The SQL statements are as follows:

UPDATE Project SET Project.project_worktime=(SELECTSUM(member_worktime)                                from projectmember                              WHERE projectmember. PID=project. ID)

Execution Result:

Modify the Projectmember table the third data is 20.

Execute the SQL statement once:

Note the rows affected, the first is 3, because 3 data is updated, the second is 1, and only one of the data in the Projectmember table that corresponds to the project table is modified. Therefore, there is no repeat update, only the changed data is updated.

Modify the Projectmember table second to third data to execute the SQL statement again, the affected rows are still one.

Modifying the third to fourth data to execute the SQL statement again, the affected rows become two.

MySQL extracts data from one table update another table (fix inconsistent table data)

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.