create table test.tmp_is_primary_phone ( id integer unsigned ); insert into test.tmp_is_primary_phone select min(id) from civicrm_phone where is_primary = 1 group by contact_id ; update civicrm_phone set is_primary = 0 where id not in ( select id from test.tmp_is_primary_phone ) ; drop table test.tmp_is_primary_phone ;