Technology and General Blog

Group _CONCAT function is a Group BY aggregate purpose that will allow you to concatenate column values from a number of rows into a solitary discipline. It returns a string if the set team contains a single or no-null column benefit and returns a NULL value if none can be found.

This tutorial will teach you how to use MySQL Group_CONCAT() operate to combine strings from a team with many alternatives.

Standard Use

As we have described, this perform returns a string outcome with the values of the concatenated non-null values or a NULL if none exists.

The normal syntax is:

Group_CONCAT([DISTINCT] expr [,expr …]
             [ORDER BY unsigned_integer
                 [ASC | DESC] [,col_name …]]
             [SEPARATOR str_val])


From the above syntax, you can see that the Group_CONCAT purpose makes use of MySQL clauses and constraints to specify the numerous options:

  1. Distinctive: The Distinct clause allows take away the duplicate values in the established team bef0re the concatenation approach. Think about our tutorial detailing MySQL Distinctive to recognize how it performs.
  2. Purchase BY: The next clause is the Buy BY that aids type the values in a specified order. The buy can both be ascending or descending. If no buy is specified, MySQL formats the values in ascending order.
  3. SEPARATOR: This clause sets the string literal inserted among the group’s values upon the concatenation course of action. By default, MySQL employs a comma (,) to different the values.

Notice: The string consequence generated by MySQL Group_CONCAT() perform is minimal to a size the price set in the team_concat_max_len variable. This worth is defined in the technique and has a default worth of 1024. You can improve this benefit globally or set it in the session you need to have.

Take into account the reference beneath to understand extra:

How It Operates: Instance

Allow me to use a easy illustration to clarify how the Team_CONCAT() functionality is effective. Contemplate the desk with a area for CHAR as:

Let us insert values into the table as shown in the query beneath:

If we complete a essential Group_CONCAT operation on the values in the desk, we will get a string result as proven under:

The resulting benefit is:

| Group_CONCAT(Distinctive worth Purchase BY worth ASC SEPARATOR ” “) |
| E H L O |
1 row in established (.01 sec)

Do you want another way to fully grasp what took place to the result supplied higher than?

We commence by taking away all the copy values thanks to the MySQL Distinctive clause that gets rid of a person L.

Upcoming, we proceed to Purchase BY ascending purchase as described in (ASC), which alters the string in the sort of

Last but not least, we execute the concatenation approach using a place as the separator for the established values, ensuing in the string E H L O from H, E, L, L O.

Instance Use Instances

Enable us take a genuine databases and use it to illustrate how we can implement the Team_CONCAT() operate. In this case in point, we will use the Sakila databases, and exclusively, the addresses desk from the Sakila databases.

Think about the useful resource underneath to obtain the database for your examples:

In the addresses table of the Sakila database, you will get the district column. We can get all the special districts divided by a pipe as shown in the query underneath:

The query higher than will show all the Distinct districts and get them in ascending order divided by a pipe.

Note: The Group_CONCAT() purpose is an aggregate functionality. Thus, it is demanded that you specify the Get BY statement within the functionality and not in the Decide on statement.


MySQL Team_CONCAT() operate talked about in this tutorial is a practical operate that allows you to produce exceptional, sorted, and structured info from a table that may well include duplicates and unordered data.

Take into consideration the docs or our other MySQL tutorials to find out much more.

Leave a Reply

Your email address will not be published. Required fields are marked *