以前我們?cè)趯憉pdate的時(shí)候往往是使用 update tablename set columnname = value這樣的簡單語法,而今天遇到一個(gè)需求,根據(jù)一張表中的數(shù)據(jù)來更新另外一張表中的某些字段值,比如有個(gè)A表和B表,A中有字段id,name,email,phone,cno,這里的cno也是唯一的,B表中有id,othercloumn,email,phone,cno,這時(shí)候想通過B中的email和phone值來更新A中的email和phone值。我們可以使用一下語句實(shí)現(xiàn)這個(gè)功能:
update A, B set
A.email= B.email, A.phone= B.phone
where A.cno=B.cno
Oracle中可以如下方式實(shí)現(xiàn):
update A set
(email, phone) = (select B.email, B.phone where B.cno= A.cno)