user1226230
user1226230

Reputation: 381

SQL/ MySQL: Multiple rows AND multiple columns into single result set

I have a Table with 3 VARCHARs in 3 different columns:

         user    owner   initiatorUser
------------------------------------
row1     UserA,  UserC,  UserC
row2     UserB,  UserC,  UserA

Now I need every occurence in all rows and in all the 3 columns as distinct VARCHAR result set or, if not possible, as a CSV.

UserA
UserB  
UserC

I know that was discussed quite often here, but none of the solutions applies for me. I experimented with MYSQL's GROUP_CONCAT, but couldn't get a fitting result.

Thanks for any help !
Gerry

Upvotes: 0

Views: 154

Answers (1)

Ergest Basha
Ergest Basha

Reputation: 8973

Based on the expected result you can use UNION.

If the coma after the values isn't a typo use:

select replace(user,',','') as distinct_
from my_table
union
select replace(owner,',','')
from my_table
union
select initiatorUser
from my_table

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=1d4d8988f7ed2a7200bab1530a7a3dfa

If it is a typo use:

select user as distinct_
from my_table
union
select owner
from my_table
union
select initiatorUser
from my_table

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=d7eeab79bf33689965ea3cf35a2f98ca

Upvotes: 1

Related Questions