为不同的员工更新managerId

时间:2015-01-06 07:24:03

标签: java mysql sql jsp select

我有Reporting_officers表的Table.structure在下面

R_id    User_id Manager_id  Managre2_id
1        4       3        3
2        3       2        2
3        5       3        2

现在我想在此表中更新user_id 3到6,然后它应该反映到manager_id和manger2_id,它们有3个 如下所示

R_id    User_id   Manager_id  Managre2_id
 1          4         6           6
 2          6         2           2
 3          5         6           2

2 个答案:

答案 0 :(得分:0)

最简单的方法是使用具有on update cascade子句的外键定义表,这样数据库就可以为您处理所有繁重的工作,而您需要做的就是更新{{1 }}:

user_id

答案 1 :(得分:0)

这个查询工作正常,你可以在sql小提琴上查看它 table strutured

table after dml

UPDATE 
  `Reporting_officers`  
SET
  `Manager_id` = NULL
WHERE Reporting_officers.`Manager_id` = 3;

UPDATE 
  `Reporting_officers`  
SET
  `Managre2_id` = NULL
WHERE `Managre2_id` = 3;


UPDATE 
  `Reporting_officers`  
SET
  User_id = 6 
WHERE `User_id` = 3;


UPDATE 
  `Reporting_officers` 
SET
  `Manager_id` = CASE WHEN Manager_id IS NULL  THEN 6 ELSE Manager_id END,
  `Managre2_id` = CASE WHEN Managre2_id IS NULL  THEN 6 ELSE Managre2_id END