Oracle中如何?Mysql的兩表關聯update操作

來源:互聯網
上載者:User

標籤:

在看《MySQL 5.1參考手冊》的時候,發現MySQL提供了一種兩表關聯update操作。原文如下:

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

在MySQL中構造表驗證了一下

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)

不難看出,上述兩表關聯update中只更新了test中id為1和id為2的行。

 

尋思了一下,Oracle中好像並沒有提供兩表關聯的update操作,同樣輸入了上述語句,Oracle報錯,報錯資訊如下:

SQL> update test,test1 set test.salary=test1.salary where test.id=test1.id;update test,test1 set test.salary=test1.salary where test.id=test1.id           *第 1 行出現錯誤:ORA-00971: 缺失 SET 關鍵字

後來,查了查官方文檔,這種文法並不支援,那麼Oracle中如何?MySQL中的這種效果呢?

鼓搗了一下,雖然出來了,但還是略為複雜。

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);已更新2行。SQL> select * from test; ID SALARY---------- ---------- 1 400 2 500 3 300

 

Oracle中如何?Mysql的兩表關聯update操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.