UPDATE statement: The statement _ MySQL

Source: Internet
Author: User
UPDATE statement: UPDATE the fields in one table to the statement bitsCN.com in another table.

UPDATE statement: The statement used to UPDATE fields in one table to fields in another table.

In a modification to the score query system, a table is changed and a field is added to a table. now, you need to copy the corresponding ID of another table, because such SQL operations are not often used, I even thought of writing a loop to process them. However, I thought that SQL should be able to implement such a function. I checked the manual and it was really simple. there was a syntax for updating... from. Example:

Table

Id subject_id

1 null

2 null

3 null

4 null

5 null

6 null

Table B

Sb_id st_id

5 1

2 2

5 3

5 4

2 5

2 6

The SQL statement is as follows:

1

UPDATEASETA. subject_id = B. sb_idFROMBWHEREB.student_id = A. id

Update: the preceding statement is only applicable to mssql server and the following statement should be used in mysql:

1

UPDATEA, BSETA. subject_id = B. sb_idWHEREB.student_id = A. id

Example 2:

MYSQL is

Update table2 B, (select B. area_id as arid, sum (a. user_amount) as bcount

From table1 a, table2 B

Where a. user_area = B. area_id

Group by arid) c

Set B. count = c. bcount

Where B. area_id = c. arid;

I also installed a MYSQL test and the test passed.

BitsCN.com

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.