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
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.