How to associate two Mysql tables with update in Oracle, oraclemysql

Source: Internet
Author: User

How to associate two Mysql tables with update in Oracle, oraclemysql

When reading MySQL 5.1 reference manual, we found that MySQL provides a two-table join update operation. The original article is as follows:

UPDATE items,month SET items.price=month.priceWHERE items.id=month.id;

I constructed a table in MySQL to verify it.

mysql> select * from test;+------+--------+| id   | salary |+------+--------+|    1 |    100 ||    2 |    200 ||    3 |    300 |+------+--------+3 rows in set (0.00 sec)mysql> select * from test1;+------+--------+| id   | salary |+------+--------+|    1 |    400 ||    2 |    500 |+------+--------+2 rows in set (0.00 sec)mysql> update test,test1 set test.salary=test1.salary where test.id=test1.id;Query OK, 2 rows affected (0.00 sec)Rows matched: 2  Changed: 2  Warnings: 0mysql> select * from test;+------+--------+| id   | salary |+------+--------+|    1 |    400 ||    2 |    500 ||    3 |    300 |+------+--------+3 rows in set (0.00 sec)

As you can see, only the rows with id 1 and id 2 in test are updated in the associated update of the two tables.

 

After thinking about it, Oracle does not seem to provide the update operation associated with the two tables. If the above statement is entered, Oracle Reports an error and the error message is as follows:

SQL> update test, test1 set test. salary = test1.salary where test. id = test1.id; update test, test1 set test. salary = test1.salary where test. line id = test1.id * 1st error: ORA-00971: Missing SET keyword

Later, I checked the official documents and found that this syntax is not supported. How can I achieve this effect in MySQL in Oracle?

It was a bit complicated, even though it came out.

SQL> update test set test. salary = (select salary from test1 where test1.id = test. id)
Where exists (select 1 from test1 where test1.id = test. id); two rows have been updated. SQL> select * from test; ID SALARY ---------- 1 400 2 500 3 300

 

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.