Skip to content

Instantly share code, notes, and snippets.

@Ocramius
Last active December 14, 2015 01:09
Show Gist options
  • Save Ocramius/5003948 to your computer and use it in GitHub Desktop.
Save Ocramius/5003948 to your computer and use it in GitHub Desktop.
SQL: Delete all but last items in groups of elements (grouped by common field) and incremental identifier
DELETE FROM
blah
WHERE
id NOT IN (
SELECT
c.id
FROM
blah c
LEFT JOIN
blah d
ON (
c.common_field = d.common_field
AND c.id < d.id
)
WHERE
d.id IS NUL
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment