开发者

Getting around MySql Error 1093 with a NOT IN Subquery

How can I accomplish what I'm trying to accomplish with the following query?

DELETE SomeDB.outertable 
FROM SomeDB.logging AS outertable
WHERE outertable.log_id NOT IN
    (
    SELECT SomeDB.f.log_id
    FROM
        (
        SELECT aa.*, MAX(aa.log_id) AS max_log_id
        FROM SomeDB.logging AS aa
        WHERE aa.operation_status = -1
        GROUP BY aa.process_id

        UNION ALL
        SELECT bb.*, MAX(bb.log_id) AS max_log_id
        FROM SomeDB.logging AS bb
        WHERE bb.operation_status = 1
        GROUP BY bb.process_id

        UNION ALL
        SELECT cc.*, MAX(c开发者_如何学Goc.log_id) AS max_log_id
        FROM SomeDB.logging AS cc
        WHERE cc.operation_status = 0
        GROUP BY cc.process_id
        )
    AS x
    INNER JOIN SomeDB.logging AS f
        ON f.process_id = x.process_id
        AND f.log_id = x.max_log_id
    )

I've tried several variants of this, based on a bit of research around the web, but I always get the following error:

Error Code: 1093 You can't specify target table 'outertable' for update in FROM clause

Edit: I'm trying to clean up an event logging table by deleting all but the very last record of each type for each process.


Remove the SomeDB.outertable from your query so it starts

DELETE FROM SomeDB.logging AS outertable
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜