Search code examples
mysqlline-breaksgroup-concat

Display each value on new lines instead of same using group_concat


I have the following Query to display the results:

SELECT C.Customer_ID
     , group_concat(CP.Company_description separator '\n') as companyName
  FROM tbl_Customer AS C
  JOIN tbl_Company AS CP
    ON (CP.Customer_ID = C.Customer_ID)
 group 
    by Customer_ID

I am getting the desired result however the results are being displayed as this.

Customer_ID companyName
1 test1, test2, test3
2 test4, test5, test6

I would like the results to display on new lines like this

Please Click Here to Refer to What I am Looking for

so just to be more clear, i was test1, test2, test3 displayed on new line like:

test1

test2

test3


Solution

  • Just add another column in group. Like this

    SELECT C.Customer_ID, CP.Company_description as companyName

    FROM tbl_Customer AS C

    inner JOIN tbl_Company AS CP

    ON (CP.Customer_ID = C.Customer_ID)

    group by Customer_ID, CP.Company_description