4 10 用其他表中的值更新

2022-06-07 23:21:14 字數 605 閱讀 2256

問題:

要用乙個表中的值來更新另外乙個表中的行。例如,在表new_sal中儲存著某個特定員工的新工資.

在表new_sal中,deptno為關鍵字。要用表new_sal中的值更新表emp中相應員工的工資,條件是emp.deptno與new_sal.deptno相等,將匹配記錄的emp.sal更新為new_sal.sal,將emp.comm更新為new_sal.sal的50%。

解決方案:

在表new_sal和emp之間做鏈結,用來查詢comm新值並帶給update語句。像這樣通過關聯子查詢進行更新的情況十分常見;另一種方法是建立檢視(建立檢視或內聯檢視,視資料庫支援而定),然後更新這個檢視。

使用相關的子查詢來設定表emp中的sal和comm值,同樣使用相關的子查詢判別表emp中哪些行需要被更新:

update emp e 

set (e.sal,e.comm)  = (select ns.sal,ns.sal/2 from new_sal ns 

where ns.deptno = e.deptno)

where exists(select null from new_sal ns 

where ns.deptno = e.deptno)

用乙個表中的一列值更新另外一表中的一列值

今天寫乙個小儲存過程,呵呵!功能大家試一下就知道了。create proc proc test uid int 0 output,uname nvarchar 50 null output asbegin declare dd cursor scroll for select uid,uname f...

activiti中其他的表

這裡只有乙個表act evt log。act evt log 時間日誌表。字段描述 型別not null 備註log nr 主鍵number 19 yestype 型別nvarchar2 64 proc def id 流程定義id nvarchar2 64 proc inst id 流程例項id n...

mysql 更新 排名 更新MySQL表中的排名

一種選擇是使用排名變數,例如 update player join select p.playerid,currank currank 1 as rank from player p join select currank 0 r order by p.points desc ranks on ra...