嗨想运行这样的查询:
UPDATE `stories` SET `position`=(select @i:=@i+1)
WHERE `topic` IN
(SELECT `topic` FROM `stories` WHERE `newstype`='2' GROUP BY `topic`)
但是目标和目的地是同一个表,并且 mysql 不允许我运行它。 我该如何实现它?
最佳答案
您可以模拟内部联接并仅更新到第一个表,例如
set @pos:=0;
update
stories a,
(select topic, @pos:=@pos+1 as new_position
from stories
where newstype=2 group by topic
) as b
set a.position=b.new_position
where a.topic=b.topic;
关于mysql - mysql中更新和选择同一个表问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4573757/