Deleting Duplicates in MySQL

后端 未结 4 1611
清歌不尽
清歌不尽 2020-12-04 03:17

I have a table like this

userid  visitorid   time
1       10          2009-12-23
1       18          2009-12-06
1       18          2009-12-14
1       18             


        
4条回答
  •  情话喂你
    2020-12-04 03:59

    You need to work around MySQL bug#6980, with a doubly nested subquery:

    DELETE FROM foo_table
    WHERE foo_table.time IN (
        SELECT time FROM (
            SELECT time FROM
                foo_table
                LEFT OUTER JOIN (
                    SELECT MAX(time) AS time
                    FROM foo_table
                    GROUP BY userid, visitorid
                    ) AS foo_table_keep
                        USING (time)
            WHERE
                foo_table_keep.time IS NULL
            ) AS foo_table_delete
        );
    

    Using GROUP BY collapses duplicates down to a single row, and MAX(time) chooses which value you want. Use another aggregate function than MAX if you want.

    Wrapping the subquery twice, providing aliases for each, avoids the error:

    ERROR 1093 (HY000): You can't specify target table 'foo_table' for update in FROM clause
    

    and has the extra advantage that it's clearer how the statement is choosing what to keep.

提交回复
热议问题