Skip to content

Instantly share code, notes, and snippets.

@mattwells
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattwells/fe4c3fd19e323a9b86fa to your computer and use it in GitHub Desktop.
Save mattwells/fe4c3fd19e323a9b86fa to your computer and use it in GitHub Desktop.
Fast query to find orphaned rows
SELECT `id`, COUNT(`id`) as `count`
FROM (
SELECT `id`
FROM `items`
UNION ALL
SELECT `id`
FROM `related`
) AS `union`
GROUP BY `id`
HAVING `count` = 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment