【发布时间】:2018-07-23 16:29:54
【问题描述】:
我有一个Employee 表,其中DEPTCODE 为空。
ID NAME AGE DEPTID DEPTCODE
---- ---------- ----- ---------- -------
1 Paul 32 2
2 Allen 25 1
还有一个像这样的Department 表:
ID DEPTNAME DEPTCODE
---- ---------- -----
1 HR DEP-01
2 ADMIN DEP-02
如何通过从Department 表中查询DEPTCODE 来更新Employee 表中的DEPTCODE?
这个我试过了;
DO $$
BEGIN
FOR depart IN
SELECT * FROM schema."Department"
LOOP
Update table schema."Employee" set "DEPTCODE"=depart."DEPTCODE"
where "DEPTID"=depart."ID";
END LOOP;
END; $$
【问题讨论】:
-
你真的还在使用 Postgres 9.1 吗?如果是,您应该立即计划升级。不再支持该版本
标签: sql postgresql join sql-update postgresql-9.1