INSERT INTO closures (ancestor, descendant, pathlength) SELECT ancestor, id, pathlength+1 FROM closures JOIN nodes ON parent_id = descendant -- what about (id, id, 0) ?