我正在尝试找出一个sql语句来删除表中的重复记录,但最多允许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 :(得分: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
它使用某种顺序的行编号,请注意,只有当date
和ip
对是唯一的时,它才会起作用。