目标表不可更新

时间:2013-12-23 18:56:02

标签: mysql sql

我有以下查询

 UPDATE (SELECT user_id,method_id FROM User AS U
                JOIN Address AS A 
                  ON A.user_id=U.id
                JOIN Shipping AS S 
                  ON S.address_id= A.id  WHERE user_id=1) AS T
 SET T.method_id=5;    

我收到错误“UPDATE的目标表T不可更新”。如何更改查询以从子查询更新列?任何解释都会很棒 我正在使用MySQL

1 个答案:

答案 0 :(得分:3)

UPDATE Shipping s
JOIN Address AS a ON s.address_id = a.id
SET s.method_id = 5
WHERE a.user_id = 1

由于User位于user_id表中,因此无需加入Address表。如果您确实需要在更新之前检查用户是否存在,则可以是:

UPDATE Shipping s
JOIN Address AS a ON s.address_id = a.id
JOIN User AS u ON a.user_id = u.id
SET s.method_id = 5
WHERE u.id = 1