消息表
员工表
如果Message表中的EmployeeID正确但PersonID全部为NULL,我如何将Message表中的每个PersonID更新为employee表查找中的相应ID?
我正在寻找......
消息
员工
要
消息
答案 0 :(得分:1)
UPDATE
需要JOIN
。但是每个RDBMS的语法都不同。
对于SQL Server:
UPDATE m
SET m.PersonID = e.PersonID
FROM Message AS m
INNER JOIN Employee AS e ON m.EmployeeID = e.ID;
WHERE m.PersonID IS NULL;
对于MySQL:
UPDATE Message AS m
INNER JOIN Employee AS e ON m.EmployeeID = e.ID;
WHERE m.PersonID IS NULL
SET m.PersonID = e.PersonID;
答案 1 :(得分:0)
标准方法使用相关子查询:
update message
set personid = (select e.personid from employees e where e.id = message.employeeid)
where personid is null;