2012-02-05 60 views
1

我試圖找出一條SQL語句從表中刪除重複的記錄,但允許多達10個重複。從MySQL表中刪除重複項,但最多允許10

我可以做下面的查詢找到這些重複的,但我不知道該怎麼做查詢,刪除所有,但第10個副本:

SELECT 
    ip, 
    date, 
    count(ip) as count 
FROM 
    table 
WHERE 
    date 
     BETWEEN 
      '2012-02-03 00-00-00' 
     AND 
      '2012-02-03 23:59:59' 
group by 
    ip 
having 
    count > 10 

我不知道這甚至有可能, 有人可以幫我嗎?

+0

還應該注意,我沒有主鍵或auto_increment,只是日期/ ip的索引 – Joe 2012-02-05 08:57:30

回答

0

試試這個:

delete t2 
from table t2 
join (
    SELECT ip, date, (
      select count(*) from 
      table t1 
      where t1.ip = t.ip and t1.date <= t.date and 
       t1.date BETWEEN '2012-02-03 00-00-00' AND '2012-02-03 23:59:59' 
     ) as row 
    FROM table t 
    WHERE date BETWEEN '2012-02-03 00-00-00' AND '2012-02-03 23:59:59' 
) t3 on t2.date = t3.date and t2.ip = t3.ip and t3.row > 10 

它採用某種順序排的編號,請注意,您去上班只有對dateip是獨一無二的。