update from

在某些情况下,我们需要update数据,但数据来自其他表,通常我们是这样

UPDATE table1 alias
SET (column_name, column_name ) = (
SELECT (column_name, column_name)
FROM table2
WHERE column_name = alias.column_name)
WHERE column_name = VALUE;

但更新的列太多的话,那就非常麻烦。。

在MS SQL里面,我们可以这样

UPDATE FT_1_49E74BA98FF7886574E1
SET
  FN01 = v.su00, -- FN01、姓名
  FN02 = v.su36, -- FN02、部门
  FN03 = v.su34, -- FN03、岗位
  FN04 = v.su21 --FN04、入职时间
FROM FE_BASE5..GROUP_USER_V AS v
WHERE FN09 = v.su01
      AND FN09 = 'AC0001';

Oracle 和 DB2 可以这样

UPDATE A SET (A1, A2, A3) = (SELECT B1, B2, B3 FROM B WHERE A.ID = B.ID)

There are no comment yet.

HTML tag cannot be used in this comment.