2

我有这样的疑问

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

这个错误有什么解决办法吗?
基本上我正在尝试的是删除grouper在删除中使用 select 标记的一组表行。我也很高兴看到与此不同的其他解决方案或想法。

sql小提琴: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))
4

1 回答 1

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。

编辑 - 括号有点偏离,我想我现在有了。老实说,这真的可以清理为一个select语句,而不是这里的嵌套版本。

于 2013-09-23T17:58:06.683 回答