I use a MySQL select query that gets the names and address of people for address labels. More than one person may live at the same address so I do a GROUP CONCAT to combine their names along with one address.
That correctly gives me salutations like
- Mrs Sally G Bloggs & Mr Fred Smith
which is good, but it also give me
- Mrs Jean Brown & Mr Harry Brown
which is bad as the surname is the same yet it gets repeated.
Is there a way to do the GROUP_CONCAT line so that I get duplicate surnames only shown once eg
- Mrs Sally G Bloggs & Mr Fred Smith
- Mrs Jean & Mr Harry Brown
The important bit of the sql I use is
SELECT
GROUP_CONCAT(
CONCAT_WS ( ' ', title , forname_1 , forename_2, surname )
SEPARATOR ' & '
) AS Salutation,
addresses.the_address
FROM
people
JOIN
addresses
ON
addresses.address_id = people.address_id
GROUP BY
people_address_id
(This post mysql GROUP_CONCAT duplicates showed how to remove duplicates from a Group_concat involving just one field but I cannot see how to do it using part of a multi field concat and this one mysql GROUP_CONCAT DISTINCT multiple columns gives a solution but I get a syntax error using it as
GROUP_CONCAT(DISTINCT CONCAT_WS ( ' ', title , forname_1 , forename_2, surname ) GROUP BY surname SEPARATOR ' & ') AS Salutation,