删除mysql中的重复行,保留XX行

I want to keep the 10 latest duplicate rows and delete all the others.

I'm using the below code, but it deletes all the records except for one.

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

Please try this query

This code will remove all duplicate row who have more then 10 duplicates.

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 
)

Try this code that will work.

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
  )