Using GROUP_CONCAT with CONCAT to concatenate strings from group in MySQL

comunidadmexicana
 
on Dec 07, 2020 10:28 PM
1601 Views

Hi,

Aggregate function GROUP_CONCAT in MySQL

These are values from my table of db MySQL 5.5.62 version

31A 31C 32B 32D 52G

I use the MySQL GROUP_CONCAT() function is an aggregate function that concatenates strings from a group into a single string with various options for this return '31A', '31C', '32B', '32D', '52G'

This is the query

SELECT
    GROUP_CONCAT( DISTINCT CONCAT( '''', xCOD ) ORDER BY xCOD ASC SEPARATOR ''', ' ) AS x_group
FROM `xtbl`
ORDER BY xCod ASC;

| x_group |

+----------------------------------+

| '31A', '31C', '32B', '32D', '52G |

+----------------------------------+

1 row in set (2.16 sec)

But on the return the last element of MySQL `GROUP_CONCAT()` function don't have the single quote '52G

How to do resolve this?

Thanks in advance for any help.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Dec 08, 2020 03:30 AM

Hi comunidadmexi...,

Use below query.

SELECT 
    CONCAT('''', GROUP_CONCAT(DISTINCT xCOD ORDER BY xCOD ASC
    SEPARATOR ''','''),'''') AS x_group
FROM `xtbl`
GROUP BY xCOD
ORDER BY xCod ASC;