Just add a group by inco_person.personid
and read the phone numbers via group_concat
GROUPING
Aggregating many tables and displaying certain columns as one
-
21-04-2022 - |
Domanda
I've got a problem with understanding php-mysql querying. Using this kind of query I try to print out all persons (inco_person) with emails (inco_phone) and phone numbers assigned to them (inco_email).
But, what if there's more than one phone number and email address relative to one person id?
Now I get 4 rows for one person with different sets of emails and phone numbers. Is there any way, to aggregate certain fields, if there's more than one?
$gsf = mysqli_query( $connector2 ,"SELECT * FROM inco_person
LEFT JOIN inco_person_position ON inco_person_position.person_id=inco_person.personid
LEFT JOIN inco_position ON inco_position.positionid=inco_person_position.position_id
LEFT JOIN inco_basic ON inco_basic.basicid=inco_person_position.company_id
LEFT JOIN inco_phone ON inco_phone.pp_id=inco_person_position.ppid
LEFT JOIN inco_email ON inco_email.pp_id=inco_person_position.ppid
ORDER BY inco_person.personid") or die(mysqli_error($connector2));
Result:
ID Firstname Lastname Position E-Mail Phone
1 Jacek Kowalski President 68-3284099 dyrektor@abc.com
1 Jacek Kowalski President 661320993 j.kowalski@abc.com
1 Jacek Kowalski President 661320993 dyrektor@abc.com
1 Jacek Kowalski President 68-3284099 j.kowalski@abc.com
Nessuna soluzione corretta
Altri suggerimenti
You could use GROUP BY
and GROUP_CONCAT
SELECT inco_person.personid, GROUP_CONCAT(inco_email.email SEPARATOR ' ') FROM inco_person
LEFT JOIN inco_person_position ON inco_person_position.person_id=inco_person.personid
LEFT JOIN inco_position ON inco_position.positionid=inco_person_position.position_id
LEFT JOIN inco_basic ON inco_basic.basicid=inco_person_position.company_id
LEFT JOIN inco_phone ON inco_phone.pp_id=inco_person_position.ppid
LEFT JOIN inco_email ON inco_email.pp_id=inco_person_position.ppid
GROUP BY inco_person_position.person_id ORDER BY inco_person.personid
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow