2016-06-01 10 views
-2

Ich habe eine Mysql-Abfrage:Convert Ergebnis mysql query in verschachteltem PHP-Array

SELECT 
    `pcm`.`ContactMechanismId` AS `ContactMechanismId`, 
    `tel`.`AreaCode` AS `AreaCode`, 
    `tel`.`PhoneNbr` AS `PhoneNbr`, 
    `tel`.`Extension` AS `Extension`, 
    `cmt`.`ContactMechanismTypeName` AS `ContactMechanismTypeName`, 
    `c`.`DialingCode` AS `DialingCode`, 
    `rt`.`RoleTypeName` AS `RoleTypeName`, 
    `cmpt`.`ContactMechanismPurposeTypeName` AS `ContactMechanismPurposeTypeName` 
FROM 
    `PartyContactMechanisms` AS `pcm` 
     INNER JOIN 
    `Telephones` AS `tel` ON `pcm`.`ContactMechanismId` = `tel`.`ContactMechanismId` 
     INNER JOIN 
    `ContactMechanisms` AS `cm` ON `cm`.`ContactMechanismId` = `pcm`.`ContactMechanismId` 
     INNER JOIN 
    `ContactMechanismTypes` AS `cmt` ON `cmt`.`ContactMechanismTypeId` = `cm`.`ContactMechanismTypeId` 
     INNER JOIN 
    `Countries` AS `c` ON `tel`.`GeoId` = `c`.`GeoId` 
     INNER JOIN 
    `PartyContactMechanismPurposes` AS `pcmp` ON `pcm`.`ContactMechanismId` = `pcmp`.`ContactMechanismId` 
     INNER JOIN 
    `RoleTypes` AS `rt` ON `rt`.`RoleTypeId` = `pcm`.`RoleTypeId` 
     INNER JOIN 
    `ContactMechanismPurposeTypes` AS `cmpt` ON `pcmp`.`PurposeTypeId` = `cmpt`.`PurposeTypeId` 

Das Ergebnis, das ich von Mysql erhalten wird

+--------------------+----------+----------+-----------+--------------------------+-------------+---------------+---------------------------------+ 
| ContactMechanismId | AreaCode | PhoneNbr | Extension | ContactMechanismTypeName | DialingCode | RoleTypeName | ContactMechanismPurposeTypeName | 
+--------------------+----------+----------+-----------+--------------------------+-------------+---------------+---------------------------------+ 
|     2 | 111  | 22222222 | 2222  | landline     |   375 | customer  | common phone     | 
|     2 | 111  | 22222222 | 2222  | landline     |   375 | contractor | common phone     | 
|     2 | 111  | 22222222 | 2222  | landline     |   375 | manufacturer | common phone     | 
|     2 | 111  | 22222222 | 2222  | landline     |   375 | customer  | primary phone     | 
|     2 | 111  | 22222222 | 2222  | landline     |   375 | contractor | primary phone     | 
|     2 | 111  | 22222222 | 2222  | landline     |   375 | manufacturer | primary phone     | 
|     2 | 111  | 22222222 | 2222  | landline     |   375 | customer  | secretary phone     | 
|     2 | 111  | 22222222 | 2222  | landline     |   375 | contractor | secretary phone     | 
|     2 | 111  | 22222222 | 2222  | landline     |   375 | manufacturer | secretary phone     | 
|     1 | 17  | 2905950 |   | landline     |   375 | customer  | other phone      | 
+--------------------+----------+----------+-----------+--------------------------+-------------+---------------+---------------------------------+ 

ich brauche, ist das obige Ergebnis in den folgenden umwandeln Array:

[ 
    [ContactMechanismId, AreaCode, PhoneNbr, Extension, ContactMechanismTypeName, DialingCode, RoleTypeName[], ContactMechanismPurposeTypeName[] 
] 

In dem Arrays RoleTypeName[] und ContactMechanismPurposeTypeName[] geht alles mögliche val ues für eine bestimmte ContactMechanismId aus der entsprechenden Spalte des mysql-Ergebnisses.

Wie kann ich das tun?

+0

post Ihren MySQL-Code hier –

+0

bieten Sie Ihre Tabellenstruktur und Abfrage haben Sie – Alex

+0

Uuh, ich werde es versuchen. Problem ist, dass es ein sehr vereinfachtes Modell von dem ist, was wirklich passiert ... :) –

Antwort

1

Sie können die select-Anweisung wie unten ändern mit GROUP_CONCAT()

SELECT 
`pcm`.`ContactMechanismId` AS `ContactMechanismId`, 
`tel`.`AreaCode` AS `AreaCode`, 
`tel`.`PhoneNbr` AS `PhoneNbr`, 
`tel`.`Extension` AS `Extension`, 
`cmt`.`ContactMechanismTypeName` AS `ContactMechanismTypeName`, 
`c`.`DialingCode` AS `DialingCode`,GROUP_CONCAT(
`rt`.`RoleTypeName`) AS `RoleTypeName`,GROUP_CONCAT(
`cmpt`.`ContactMechanismPurposeTypeName`) AS `ContactMechanismPurposeTypeName` 

Dann gruppieren Sie die Daten GROUP BY verwenden, werden Sie erhalten RoleTypeName und ContactMechanismPurposeTypeName als ein Komma getrennt value.You können PHP-Schleife iterieren und unter Verwendung explode() Funktion um das Array zu bilden

+0

danke! Ich bin mir nicht ganz sicher, wie man 'group by' in diesem Fall benutzt, denn wenn ich' GROUP BY ContactMechanismPurposeTypeName, RoleTypeName' am Ende des ganzen sql benutze, bekomme ich das selbe Ergebnis, als hätte ich 'group_concat () 'und' gruppieren 'überhaupt. Könntest du bitte klären? –

+0

ok, ich habe es selbst herausgefunden: ich musste nach 'ContactMechanismId' gruppieren und' GROUP_CONCAT (DISTINCT ...) 'verwenden. –

+0

Sorry, meine Erklärung ist ein wenig verwirrend – Shin