MySQL GROUP_CONCAT() function
has average rating
7
out of 10.
Total 17 users rated.
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.
Example :
Sample table : book_mast
Code
SELECT pub_id,GROUP_CONCAT(cate_id) FROM book_mast GROUP BY pub_id;
Explanation
The above MySQL statement will return a list of comma(,) separated 'cate_id's for each group of 'pub_id' from the book_mast table.
Output

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>
MySQL GROUP_CONCAT() with order by
Description
Here we have discussed MySQL GROUP_CONCAT() with ORDER BY.
Example
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;
Explanation
The above 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.
Output

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>
MySQL GROUP_CONCAT() with distinct
Description
Here we have discussed how to use MySQL GROUP_CONCAT() with DISTINCT.
Example
Sample table : book_mast
Code
SELECT pub_id,GROUP_CONCAT(DISTINCT cate_id) FROM book_mast GROUP BY pub_id;
Explanation
The above 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.
Output

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>
MySQL GROUP_CONCAT() with separator
Description
Here we have discussed MySQL GROUP_CONCAT() with separator.
Example :
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 ;
Explanation
The above 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.
Output

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>

