Adapted untested from untranslated selects: Fixed problem with self-referencing tables.