w3resource logo


>MySQL group_concat function

MySQL GROUP_CONCAT() function

<<PreviousNext>>

Seondary Nav

Description

MySQL GROUP_CONCAT() function returns a string with concatenated non-NULL value from a group.

Returns NULL when there are no non-NULL values.

Syntax

GROUP_CONCAT(expr);

Where expr is an expression.

MySQL Version : 5.6

Table of Contents

Example : MySQL GROUP_CONCAT() function

MySQL GROUP_CONCAT() with order by and example

MySQL GROUP_CONCAT() with distinct example

MySQL GROUP_CONCAT() with separator and example

Example : MySQL GROUP_CONCAT() function

The following MySQL statement will return a list of comma(,) separated 'cate_id's for each group of 'pub_id' from the book_mast table.

Sample table : book_mast

Code

SELECT pub_id,GROUP_CONCAT(cate_id)
FROM book_mast
GROUP BY pub_id;

Output

mysql> SELECT pub_id,GROUP_CONCAT(CATE_ID)
    -> FROM book_mast
    -> GROUP BY pub_id;
+--------+-----------------------+
| pub_id | GROUP_CONCAT(CATE_ID) |
+--------+-----------------------+
| P001   | CA002,CA004           | 
| P002   | CA003,CA003           | 
| P003   | CA001,CA003           | 
| P004   | CA005,CA002           | 
| P005   | CA001,CA004           | 
| P006   | CA005,CA001           | 
| P007   | CA005,CA002           | 
| P008   | CA005,CA004           | 
+--------+-----------------------+
8 rows in set (0.02 sec)

Pictorial Presentation

example aggregate functions and grouping group_concat

PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>example-group_concat- php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Returning the “cate_id's as a string separated by the comma(,) for each group of 'pub_id' from the book_mast table  :</h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Publisher id</td><td width='100' align='center'>GROUP_CONCAT(cate_id)</td>
";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT pub_id,GROUP_CONCAT(cate_id)
FROM book_mast
GROUP BY pub_id");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['pub_id'] . "</td>";
echo "<td align='center' width='200'>" . $row['GROUP_CONCAT(cate_id)'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

View the example in browser

Go Top

Example : MySQL GROUP_CONCAT() with order by

The following MySQL statement will return unique “cate_id”s , as a list of strings separated by the commas, in ascending order for each group of 'pub_id' from the book_mast table. The order can be changed in descending, using 'DESC' instead of 'ASC' at the end of the select statement.

Sample table : book_mast

Code

SELECT pub_id,GROUP_CONCAT(DISTINCT cate_id)
FROM book_mast
GROUP BY pub_id
ORDER BY GROUP_CONCAT(DISTINCT cate_id) ASC;

Output

mysql> SELECT pub_id,GROUP_CONCAT(DISTINCT cate_id)
    -> FROM book_mast 
    -> GROUP BY pub_id
    -> ORDER BY GROUP_CONCAT(DISTINCT cate_id) ASC;
+--------+--------------------------------+
| pub_id | GROUP_CONCAT(DISTINCT cate_id) |
+--------+--------------------------------+
| P003   | CA001,CA003                    | 
| P005   | CA001,CA004                    | 
| P001   | CA002,CA004                    | 
| P002   | CA003                          | 
| P006   | CA005,CA001                    | 
| P004   | CA005,CA002                    | 
| P007   | CA005,CA002                    | 
| P008   | CA005,CA004                    | 
+--------+--------------------------------+
8 rows in set (0.00 sec)

PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>example-group_concat-with-order-by- php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Returning unique “cate_id's, as a string separated by the comma(,) in ascending order for each group of 'pub_id' from the book_mast table.  :</h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Publisher id</td><td width='100' align='center'>GROUP_CONCAT(DISTINCT cate_id)</td>
";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT pub_id,GROUP_CONCAT(DISTINCT cate_id)
FROM book_mast
GROUP BY pub_id
ORDER BY GROUP_CONCAT(DISTINCT cate_id) ASC");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['pub_id'] . "</td>";
echo "<td align='center' width='200'>" . $row['GROUP_CONCAT(DISTINCT cate_id)'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

View the example in browser

Go Top

Example : MySQL GROUP_CONCAT() with distinct

The following MySQL statement will return the unique “cate_id”s, as a list of strings separated by the commas, for each group of 'pub_id' from the book_mast table.

Sample table : book_mast

Code

SELECT pub_id,GROUP_CONCAT(DISTINCT cate_id)
FROM book_mast
GROUP BY pub_id; 

Output

mysql> SELECT pub_id,GROUP_CONCAT(DISTINCT cate_id)
    -> FROM book_mast
    -> GROUP BY pub_id;
+--------+--------------------------------+
| pub_id | GROUP_CONCAT(DISTINCT cate_id) |
+--------+--------------------------------+
| P001   | CA002,CA004                    | 
| P002   | CA003                          | 
| P003   | CA001,CA003                    | 
| P004   | CA005,CA002                    | 
| P005   | CA001,CA004                    | 
| P006   | CA005,CA001                    | 
| P007   | CA005,CA002                    | 
| P008   | CA005,CA004                    | 
+--------+--------------------------------+
8 rows in set (0.00 sec)

PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>example-group_concat-with-distinct- php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Returning unique “cate_id's as a string separated by the comma(,) for each group of 'pub_id' from the book_mast table  :</h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Publisher id</td><td width='100' align='center'>GROUP_CONCAT(DISTINCT cate_id)</td>
";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT pub_id,GROUP_CONCAT(DISTINCT cate_id)
FROM book_mast
GROUP BY pub_id");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['pub_id'] . "</td>";
echo "<td align='center' width='200'>" . $row['GROUP_CONCAT(DISTINCT cate_id)'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

View the example in browser

Go Top

Example : MySQL GROUP_CONCAT() with separator

The following MySQL statement will return unique “cate_id”s, as a list of strings separated by the specified separator ' '(space) in ascending order for each group of 'pub_id' from the book_mast table. The order can be changed in descending, using 'DESC' option instead of 'ASC' at the end of the select statement.

Sample table : book_mast

Code

SELECT pub_id,GROUP_CONCAT(DISTINCT cate_id
ORDER BY  cate_id ASC SEPARATOR ' ')
FROM book_mast
GROUP BY pub_id ;

Output

mysql> SELECT pub_id,GROUP_CONCAT(DISTINCT cate_id
    -> ORDER BY  cate_id ASC SEPARATOR ' ')
    -> FROM book_mast
    -> GROUP BY pub_id ;
+--------+--------------------------------------------------------------------+
| pub_id | GROUP_CONCAT(DISTINCT cate_id
ORDER BY  cate_id ASC SEPARATOR ' ') |
+--------+--------------------------------------------------------------------+
| P001   | CA002 CA004                                                        | 
| P002   | CA003                                                              | 
| P003   | CA001 CA003                                                        | 
| P004   | CA002 CA005                                                        | 
| P005   | CA001 CA004                                                        | 
| P006   | CA001 CA005                                                        | 
| P007   | CA002 CA005                                                        | 
| P008   | CA004 CA005                                                        | 
+--------+--------------------------------------------------------------------+
8 rows in set (0.00 sec)

PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>example-group_concat-with-seperator- php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>return unique cate_id's, as a string separated by the specified separator ' '(space) in ascending order for each group of 'pub_id' from the book_mast table :</h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Publisher id</td><td width='100' align='center'>GROUP_CONCAT(DISTINCT cate_id
ORDER BY  cate_id ASC SEPARATOR ' ')</td>
";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT pub_id,GROUP_CONCAT(DISTINCT cate_id
ORDER BY  cate_id ASC SEPARATOR ' ')
FROM book_mast
GROUP BY pub_id");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['pub_id'] . "</td>";
echo "<td align='center' width='200'>" . $row["GROUP_CONCAT(DISTINCT cate_id
ORDER BY  cate_id ASC SEPARATOR ' ')"] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

View the example in browser

Go Top



We have compiled a number of MySQL Questions and Answers. Check and assess you MySQL skill by visiting those questions and answers.

<<PreviousNext>>

 

Looking for some other tutorial?