2013-07-15 93 views
1

您認爲像這樣的查詢會在執行我的軟件時產生問題嗎? 我需要刪除除最後2組條目之外的所有表格,這些條目按照同一時間插入進行分組。同一張表上的嵌套查詢

delete from tableA WHERE time not in 
       (
        SELECT time FROM 
        (select distinct time from tableA order by time desc limit 2 
       ) AS tmptable 
       ); 

你有更好的解決辦法嗎?我用mysql 5.5

回答

2

我看不出有什麼問題與您的查詢,但我更喜歡使用一個OUTER JOIN/NULL檢查(加上它減輕了嵌套子查詢的一個必要):

delete a 
from tableA a 
    left join 
    (
     select distinct time 
     from tableA 
     order by time desc 
     limit 2 
) b on a.time = b.time 
where b.time is null