UPDATE scrm.t_contacts t SET t.MAIN_USER_ID = ( SELECT IF ( u.`PARENT_ID` = '0', u.`ID`, u.`PARENT_ID` ) MAIN_USER_ID FROM `user-center`.tb_user u WHERE t.USER_ID = u.`ID` );