选择id列表并更新这些行

时间:2013-09-24 22:52:40

标签: mysql sql

我有以下MySQL声明:

UPDATE xxx_iop_user_plans
SET state = 1, accepted = 1
WHERE id IN (SELECT iup1.id FROM xxx_iop_user_plans iup1
INNER JOIN xxx_iop_user_ed iue ON iup1.user_id = iue.user_id
WHERE iue.parent_id = 44 AND iup1.iop_id = 11)

如何更新此语句以更新SELECT语句返回的列表中存在的ID号的行?

2 个答案:

答案 0 :(得分:1)

我原本希望你的更新声明能够做到这一点。如果它没有更新所有这些ID它在做什么?您可以重新组织查询以将select子查询放在from子句中:

UPDATE x 
SET state = 1, accepted = 1
FROM xxx_iop_user_plans x 
    INNER JOIN (
        SELECT iup1.id 
        FROM xxx_iop_user_plans iup1
            INNER JOIN xxx_iop_user_ed iue ON iup1.user_id = iue.user_id
        WHERE iue.parent_id = 44 AND iup1.iop_id = 11
    ) y ON x.ID = y.ID

但我不希望这与你已经拥有的有任何不同。还是试试看。

答案 1 :(得分:0)

您应该使用MySQL Multiple-table update syntax,如下所示:

UPDATE xxx_iop_user_plans
  INNER JOIN xxx_iop_user_ed ON xxx_iop_user_plans.user_id = xxx_iop_user_ed.user_id
SET state = 1, accepted = 1
WHERE xxx_iop_user_ed.parent_id = 44 
  AND xxx_iop_user_plans.iop_id = 11;