I have a query
SELECT
cc.contact_id,
cc.name,
ct.name AS contact_type
FROM
contacts AS c
LEFT JOIN contact_companies AS cc ON c.contact_id = cc.contact_id
LEFT JOIN contacts_to_types AS ctt ON cc.contact_id = ctt.contact_id
LEFT JOIN contact_types AS ct ON ctt.contact_type_id = ct.contact_type_id
WHERE
cc.name LIKE '%p%'
ORDER BY name, contact_id
which returns:
+------------+--------------------------------+--------------+
| contact_id | name | contact_type |
+------------+--------------------------------+--------------+
| 297 | Primary Properties Corporation | Supplier |
| 297 | Primary Properties Corporation | Prospect |
| 297 | Primary Properties Corporation | Customer |
| 298 | San Miguel Corporation | Prospect |
| 301 | Sulpicio Lines | Supplier |
+------------+--------------------------------+--------------+
when I would like it to return:
+------------+--------------------------------+------------------------------+
| contact_id | name | contact_type |
+------------+--------------------------------+------------------------------+
| 297 | Primary Properties Corporation | Supplier, Prospect, Customer |
| 298 | San Miguel Corporation | Prospect |
| 301 | Sulpicio Lines | Supplier |
+------------+--------------------------------+------------------------------+
that is, I’d like it to combine the contact_type of 297.
Is this possible? Could someone please show me how? 🙂
Thanks
Use the
GROUP_CONCAT()aggregate function