Multi-table join update in Oracle

Source: Internet
Author: User
Suppose we have two tables: test1 and Test2.

Create   Table Test1 (No Number , Name Varchar2 ( 10 ));

Create   Table Test2 (No Number , Name Varchar2 ( 10 ));

Insert   Into Test1 Values ( 1 , ' A ' );

Insert   Into Test1 Values ( 2 , ' B ' );

Insert   Into Test1 Values ( 3 , ' C ' );

Insert   Into Test2 Values ( 1 , ' AA ' );

Insert   Into Test2 Values ( 2 , ' Bb ' );

So far:

Test1

No name

--------------------

1

2 B

3 C

Test2

No name

--------------------

1 aa

2 bb

If you want to update the name field of a record in test1 and Test2 with the no field equal to that in Test2

Segment value.

That is, the following results:

Test1

No name

--------------------

1 aa

2 bb

3 C

The following statements

Update test1 a set name = (Select name from Test2 B where a. No = B. No );

The effect is:

Test1

No name

--------------------

1 aa

2 bb

3

To achieve the desired effect, you can use the following statement.
Update Test1 Set Name = ( Select Name From Test2 B Where A. No = B. No) Where

Exists ( Select Name From Test2 B Where A. No = B. No );

You can also use the following statement:
Update(SelectA. Name aname, B. Name bnameFromTest1 A, Test2 BWhere

A. No
=B. No)SetAname=Bname;

However, there is a premise that no in Table Test2 is set as the primary key.

Alter table Test2 add primary key (NO );

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.