gpt4 book ai didi

mysql - 使用自连接子查询更新

转载 作者:行者123 更新时间:2023-11-29 16:45:50 24 4
gpt4 key购买 nike

问题:

A supervisor (employee_id equals to 114) has resigned from the company, and today is the last day of work. With immediate effect, the department he manages, and all those employees he supervises will be taken over by his supervisor

所以我想出的语法是:

UPDATE EMPLOYEE
SET supervisor_id = (SELECT supervisor_id
FROM EMPLOYEE
WHERE employee_id = '114'),
WHERE supervisor_id = '114';

输出:错误 1093 (HY000):您无法在 FROM 子句中指定要更新的目标表“EMPLOYEE”

但是这是错误的。我知道supervisor_id = 100然而我不只是想把 SET supervisor_id = 100因为 100 似乎不公平直接。谁能帮我改正吗?

最佳答案

UPDATE EMPLOYEE a 
join
( SELECT supervisor_id
, employee_id
FROM EMPLOYEE
WHERE employee_id = '114'
) b
on a.supervisor_id = b.employee_id
set a.supervisor_id = b.supervisor_id

使用此查询

关于mysql - 使用自连接子查询更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53117347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com