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