UPDATE Team SET Leader=(SELECT Id FROM `user` WHERE user.Team=Team.Id LIMIT 1) WHERE Leader NOT IN (SELECT ID FROM user WHERE user.Team=Team.Id);