我有下表



ID      Person     Supervisor
1       Michele    2
2       James      4
3       Jacob      4
4       Francis    1


假设Michele(1)离开了,我希望她的Supervisor(2)接管对Michele所监管的任何人的监督(Francis,4)

SELECT id FROM TABLE WHERE supervisor = '1';


会还给我2(米歇尔)

要更新,我只会做

UPDATE TABLE
SET Supervisor = '2' <-- can this not be hard-coded but "selected" in an UPDATE statement?
WHERE Supervisor = '1';

最佳答案

使用自联接。

UPDATE TABLE AS t1
JOIN TABLE AS t2 ON t1.supervisor = t2.id
SET t1.supervisor = t2.supervisor
WHERE t2.id = 1


DEMO

关于mysql - 是否可以选择和更新未知值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58612607/

10-10 19:37