在WHERE子句中使用主键时,从同一个表上的select更新时出错

时间:2015-01-27 16:25:52

标签: mysql sql

这是我的疑问:

UPDATE status as t1
JOIN (
    SELECT status.id, status.deployment_id FROM status
) as t2
SET t1.task_id=t2.deployment_id
WHERE t1.id=t2.id;

这就是回应:

Error Code: 1175. You are using safe update mode and you tried to update a table **without a WHERE that uses a KEY column** To disable safe mode, toggle the option in Preferences -> SQL Queries and reconnect.

但我实际上在我的查询中使用主键,为什么我仍然得到这样的结果?

PS
我使用MySQL。

更新

http://sqlfiddle.com/#!9/2265f

1 个答案:

答案 0 :(得分:0)

我的猜测是问题是子查询,这是不需要的。你可以写成:

UPDATE status as t1 JOIN
       status t2
       ON t1.id = t2.id
    SET t1.task_id = t2.deployment_id;

当然,如果id是主键,那么它是唯一的而且永远不会NULL,所以写起来要简单得多:

UPDATE status s
    SET s.task_id = s.deployment_id;