我有一个Mysql查询:
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`
我从Mysql获得的结果是
+--------------------+----------+----------+-----------+--------------------------+-------------+---------------+---------------------------------+
| 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 |
+--------------------+----------+----------+-----------+--------------------------+-------------+---------------+---------------------------------+
我需要将上面的结果转换为以下数组:
[
[ContactMechanismId, AreaCode, PhoneNbr, Extension, ContactMechanismTypeName, DialingCode, RoleTypeName[], ContactMechanismPurposeTypeName[]
]
在数组RoleTypeName[]
和ContactMechanismPurposeTypeName[]
中,从mysql结果的相应列中获取某个ContactMechanismId
的所有可能值。
我该怎么做?
答案 0 :(得分:1)
您可以使用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`
然后使用GROUP BY
对数据进行分组,您将获得RoleTypeName和ContactMechanismPurposeTypeName作为逗号分隔值。您可以使用php循环迭代并使用explode()函数来形成数组