mysql select group_concat code example

Example 1: mysql group_concat distinct

SELECT GROUP_CONCAT(DISTINCT categories ORDER BY categories ASC SEPARATOR ' ') FROM table

Example 2: group by mysql and concatenate string

mysql> select Id,group_concat(Name SEPARATOR ',') as GroupConcatDemo from GroupConcatenateDemo
   -> group by Id;

Example 3: group_concat sql server

STRING_AGG ( expression, separator ) [ <order_clause> ]

<order_clause> ::=   
    WITHIN GROUP ( ORDER BY <order_by_expression_list> [ ASC | DESC ] )
    SELECT STRING_AGG(Genre, ',') AS Result
FROM Genres;
Result:

Result                                      
--------------------------------------------
Rock,Jazz,Country,Pop,Blues,Hip Hop,Rap,Punk

Tags:

Sql Example