1

我想保留 10 个最新的重复行并删除所有其他行。

我正在使用下面的代码,但它会删除除一条之外的所有记录。

DELETE FROM `history` WHERE id NOT IN (SELECT * FROM (SELECT MIN(n.id) FROM history n GROUP BY n.url) x)`
4

2 回答 2

0

请尝试此查询

此代码将删除所有重复数超过 10 个的重复行。

DELETE FROM `history` WHERE id NOT IN (
    SELECT * FROM (
        SELECT MIN(n.id) FROM history n GROUP BY n.url having count(n.url) > 10
    ) x 
)
于 2013-09-26T13:56:28.933 回答
0

试试这个可行的代码。

DELETE
FROM
  history
WHERE
  id NOT IN
  (SELECT
    id 
   FROM
     (
      SELECT
        @num:=IF(@current=url, @num+1, 1) AS row_num, 
        @current:=url AS group_url, 
        id
      FROM
        history
      ORDER BY
        id
     ) AS `internal`
   WHERE
     row_num<=15
  )
于 2013-09-26T18:01:15.180 回答