0
update moodlesocialuser 
set moodlesocialuser.deleted=deleted+1 
where id IN (
  select m1.id 
  from moodlesocialuser m1 
  join moodlesocialpost m2 on m1.id=m2.userid 
  where m2.module='blog' 
  and m1.deleted='0' 
  and url like 'http%' 
  and m1.id NOT IN (
    select id from moodlesocialcourse_display
  ) 
);

我无法进行更新并从同一张表中进行选择。

错误 1093 (HY000):您不能在 FROM 子句中指定目标表 'moodlesocialuser' 进行更新。

我怎样才能做到这一点?

4

4 回答 4

0

看起来您在该 UPDATE 语句中复制粘贴了现有查询。您可以摆脱第一个子查询中的 m1 别名,可能像这样:

update moodlesocialuser 
set moodlesocialuser.deleted=deleted+1 
where id IN (
  select m2.userid
  where m2.module='blog' 
  and url like 'http%'
)
and moodlesocialuser.deleted='0' 
and id NOT IN (
    select id from moodlesocialcourse_display
)
于 2013-07-25T08:43:57.537 回答
0

错误消息说,您正在尝试更新正在读取的表。

使用临时表:

create temporary table foo (id int);

insert into foo 
  select m1.id 
  from moodlesocialuser m1 
  join moodlesocialpost m2 on m1.id=m2.userid 
  where m2.module='blog' 
  and m1.deleted='0' 
  and url like 'http%' 
  and m1.id NOT IN (
    select id from moodlesocialcourse_display
  ) 
;

update moodlesocialuser m
inner join foo on m.id = foo.id
set m.deleted=deleted+1 
;

drop table foo;
于 2013-07-25T08:44:09.633 回答
0

您不能修改您从中选择的表(在此处阅读更多信息);另一方面,我认为您可以像这样摆脱该子查询(我认为下面的查询实际上不会起作用,您最好在不有价值的数据上对其进行测试;我想说的是-您也可以加入更新):

UPDATE moodlesocialuser mu, moodlesocialcourse_display md, moodlesocialpost mp
SET mu.deleted=mu.deleted + 1 
WHERE
md.id != mu.id AND
mu.deleted = 0 AND
mp.userid = mu.id AND
mp.module = 'blog' AND
mp.url LIKE 'http%'
于 2013-07-25T08:45:55.103 回答
0

如果此查询中没有聚合函数,为什么要使用 group by 子句。注释掉 group by 子句,脚本将运行而不会出现任何错误。

于 2013-07-25T08:32:02.293 回答