How to update a table using query results in Oracle

Source: Internet
Author: User

How to update a table using query results in Oracle

Oracle uses the results of one query to update the data of another table.

The simulation experiment is as follows. Use the query results of table t2 to update data with the same id as table t1.
 
Drop table t1;
 
Drop table t2;

Create table t1 (id int primary key, nickname varchar (20), playNum varchar (20 ));

Create table t2 (id int primary key, nickname varchar (20), playNum varchar (20 ));

Insert into t1 values (1, 1, 10 );

Insert into t1 values (2, 2, 20 );

Insert into t1 values (3, 3, 30 );

Insert into t2 values (100 );

Insert into t2 values (2, 22, 200 );

Insert into t2 values (300 );

Commit;

Oracle has two methods:
Inline view update
 
Update (
 
Select t1.id t1id, t1.nickname t1nickname, t1.playNum t1playnum, t2.id t2id, t2.nickname t2nickname, t2.playNum t2playnum

From t1 inner join t2 on (t1.id = t2.id)

)

Set t1nickname = t2nickname, t1playnum = t2playnum;

 

Related subqueries
 
Update t1 set (nickname, playNum) =
 
(Select nickname, playNum from t2 where t1.id = t2.id );

MySQL only supports the following method:
However, you can modify the data of two tables at the same time.
Update t1 with playNum of t2 and t2.

Update t1, t2
 
Set t2.nickname = t1.nickname, t1.playNum = t2.playNum

Where t1.id = t2.id;

The methods of MySQL and Oracle are not universal.

Installing Oracle 12C in Linux-6-64

Install Oracle 11gR2 (x64) in CentOS 6.4)

Steps for installing Oracle 11gR2 in vmwarevm

Install Oracle 11g XE R2 In Debian

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.