Search code examples
mysqlconcatenationgroup-concat

How to use Group_Concat() to concat in MySql


I have table structure like this-

 Code         Codelang          Name

  14           de              David
  14           en              Michel
  14           es              John

I want to show this table as-

 Code                Name

 14               [:de]David[:en]Michel[:es]John[:]

Is it possible to do this using Group_Concat() or is there any other way to do this.?


Solution

  • SELECT
      code,
      GROUP_CONCAT(CONCAT('[:',codelang,']',name) SEPARATOR '') as name
    FROM table1
    GROUP BY code
    

    to get [:] at the end you can try:

    SELECT
      code,
      CONCAT(GROUP_CONCAT(CONCAT('[:',codelang,']',name) SEPARATOR ''),'[:]') as name
    FROM table1
    GROUP BY code