SELECT p.id, p.phone_number, p.fullname FROM `package_profile` as pp left join profiles as p on p.id=pp.profile_id where pp.status=1 and p.phone_number is not null group by pp.profile_id ORDER BY `p`.`fullname` asc; 


SELECT p.id, p.phone_number, p.fullname, e.val FROM `package_profile` as pp left join profiles as p on p.id=pp.profile_id left join (SELECT p.id as ids, max(pp.status) as val FROM `package_profile` as pp left join profiles as p on p.id=pp.profile_id group by pp.profile_id ORDER BY `p`.`id` ASC) as e on e.ids=pp.profile_id where e.val=0 and p.phone_number is not null group by pp.profile_id ORDER BY `p`.`fullname` ASC; 