Mysql merges with multiple values ​​in one column

I need to make a query that creates 3 columns that come from two tables that have the following relationships:

TABLE 1 has a column identifier that refers to table 2 with column ID2

TABLE 1 has a column named user. Table 2 has a column named

There may be one unique user, but there may be many names associated with this user.

If I do the following, I will get all the data, but the user column will be repeated for every name it associates. I want the usage to appear unique, but the name columns are displayed with all the names associated with the user column, but separated by commas, for example:

select user, names from TABLE1 leave connection TABLE2 at TABLE1.id = TABLE2.id

This will show that users are repeated every time a name appears for that user. I want to look like this:

USER NAME
cyrex - pedrox, rambo, zelda
homeboy - carmen, carlos, tom, sandra
jerry - seinfeld, christine
ninja - soloboy

etc....

+5
source share
1 answer

What you are looking for is the GROUP_CONCAT statement .

select user, GROUP_CONCAT(names SEPARATOR ',')
from TABLE1 left join TABLE2 on TABLE1.id = TABLE2.id
group by user
+11
source

All Articles