1

这不起作用,我收到以下错误。我知道为什么,因为我无法在同一张表上选择和更新。有人可以帮助/指导我正确的方向吗?

update 
    episode 
set 
    update_wp = 1 
where 
    episode_id in(
        select 
            e.episode_id 
        from 
            collection c 
        inner join 
            season s on s.collection_id = c.collection_id 
        inner join 
            episode e on e.season_id = s.season_id  
        where 
            c.title ='mom'
        );

MySQL 响应:

ERROR 1093 (HY000): You can't specify target table 'episode' for update in FROM clause
4

3 回答 3

1

我宁愿使用以下查询:

UPDATE episode e, collection c, season s 
   SET e.update_wp = 1 
   WHERE 
     e.season_id = s.season_id AND 
     s.collection_id = c.collection_id AND 
     c.title = 'mom';
于 2013-10-11T23:54:41.540 回答
0

MySQL 不允许您更新您在 select 子句中使用的表。有关更多详细信息,请参阅此答案

有几种解决方法,其中之一是将 select 子句与其他 select 语句包装在一起(警告:效率低下)。

update 
    episode 
set 
    update_wp = 1 
where 
    episode_id in (
      select in_episode_id from (
        select 
            e.episode_id as in_episode_id
        from 
            collection c 
        inner join 
            season s on s.collection_id = c.collection_id 
        inner join 
            episode e on e.season_id = s.season_id  
        where 
            c.title ='mom'
      )
    ) as x;
于 2013-10-11T23:41:19.870 回答
0
    update t1
    set 
        t1.update_wp = 1 
    from episode t1
    join
    (
            select 
                e.episode_id 
            from 
                collection c 
            inner join 
                season s on s.collection_id = c.collection_id 
            inner join 
                episode e on e.season_id = s.season_id  
            where 
                c.title ='mom'
    )t2
    t1.episode_id=t2.episode_id
于 2013-10-11T23:48:56.793 回答