標籤:
在看《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操作