使用NOT IN子查询解决MySql错误1093

时间:2011-02-24 16:27:50

标签: mysql sql greatest-n-per-group mysql-error-1093

如何通过以下查询完成我想要完成的任务?

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(cc.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
    )

基于对网络的一些研究,我尝试了几种变体,但我总是得到以下错误:

错误代码:1093 您无法在FROM子句

中为更新指定目标表'outertable'

编辑:我正在尝试通过删除每个进程的每种类型的最后一条记录来清理事件记录表。

1 个答案:

答案 0 :(得分:0)

从查询中删除SomeDB.outertable以便它启动

DELETE FROM SomeDB.logging AS outertable