我有这样的疑问
SET @n=0;
DELETE t3 FROM (
SELECT id, project_id, task_id, user_id,grouper
FROM (
SELECT id, project_id, task_id, user_id,
@n:=if(status=55,@n+1,@n),
if(status=55,@n-1,@n) as grouper FROM timelog
WHERE user_id='5' ORDER BY id ASC
) as t
where grouper>-1
group by grouper) as t3 WHERE grouper=1
我收到The target table t3 of the DELETE is not updatable
这个错误有什么解决方案吗?
基本上我正在尝试的是使用select in delete删除标有grouper
的表行组。我也很高兴其他解决方案或想法与此不同。
sql fiddle:http://sqlfiddle.com/#!2/33820/2/0
编辑:感谢这里的答案是工作代码(如果有人需要类似的东西):
SET @n=0;
delete from timelog where id in ((SELECT id
FROM (
SELECT id, project_id, task_id, user_id,
@n:=if(status=55,@n+1,@n),
if(status=55,@n-1,@n) as grouper FROM timelog
WHERE user_id='5' ORDER BY id ASC
) as t
where grouper>-1 and grouper=1
group by grouper))
答案 0 :(得分:1)
希望我有更多时间......但是快速的伪代码......
delete from timelog where id in ((SELECT id
FROM (
SELECT id, project_id, task_id, user_id,
@n:=if(status=55,@n+1,@n),
if(status=55,@n-1,@n) as grouper FROM timelog
WHERE user_id='5' ORDER BY id ASC
) as t
where grouper>-1
group by grouper) as t3 WHERE grouper=1)
我所做的就是将subselect语句更改为where子句,该子句只返回原始子查询中列出的ID。
编辑 - 括号有点偏,我想我现在有了。说实话,这可以真正清理到一个选择语句,而不是这里的嵌套版本。