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>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-group_concat- php mysql examples | w3resource</title>
<meta name="description" content="example-group_concat- php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<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>
<table class='table table-bordered'>
<tr>
<th>Publisher id</th><th>GROUP_CONCAT(cate_id)</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username"; 
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT pub_id,GROUP_CONCAT(cate_id)
FROM book_mast
GROUP BY pub_id') as $row) {
echo "<tr>";
echo "<td>" . $row['pub_id'] . "</td>";
echo "<td>" . $row['GROUP_CONCAT(cate_id)'] . "</td>";
echo "</tr>"; 
}
?>
</tbody></table>
</div>
</div>
</div>
</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>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-group_concat-with-order-by- php mysql examples | w3resource</title> <meta name="description" content="example-group_concat-with-order-by- php mysql examples | w3resource"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> </head> <body> <div class="container"> <div class="row"> <div class="col-md-12"> <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> <table class='table table-bordered'> <tr> <th>Publisher id</th><th>GROUP_CONCAT(DISTINCT cate_id)</th> </tr> <?php $hostname="your_hostname"; $username="your_username"; $password="your_password"; $db = "your_dbname"; $dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password); foreach($dbh->query('SELECT pub_id,GROUP_CONCAT(DISTINCT cate_id) FROM book_mast GROUP BY pub_id ORDER BY GROUP_CONCAT(DISTINCT cate_id) ASC') as $row) { echo "<tr>"; echo "<td>" . $row['pub_id'] . "</td>"; echo "<td>" . $row['GROUP_CONCAT(DISTINCT cate_id)'] . "</td>"; echo "</tr>"; } ?> </tbody></table> </div> </div> </div> </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>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-group_concat-with-distinct- php mysql examples | w3resource</title>
<meta name="description" content="example-group_concat-with-distinct- php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<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>
<table class='table table-bordered'>
<tr>
<th>Publisher id</th><th>GROUP_CONCAT(DISTINCT cate_id)</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT pub_id,GROUP_CONCAT(DISTINCT cate_id) FROM book_mast GROUP BY pub_id') as $row) { echo "<tr>"; echo "<td>" . $row['pub_id'] . "</td>"; echo "<td>" . $row['GROUP_CONCAT(DISTINCT cate_id)'] . "</td>"; echo "</tr>"; } ?> </tbody></table> </div> </div> </div> </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>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-group_concat-with-seperator- php mysql examples | w3resource</title>
<meta name="description" content="example-group_concat-with-seperator- php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<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>
<table class='table table-bordered'>
<tr>
<th>Publisher's ID</th><th>GROUP_CONCAT(DISTINCT cate_id ORDER BY  cate_id ASC SEPARATOR ' ')</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT pub_id,GROUP_CONCAT(DISTINCT cate_id
ORDER BY  cate_id ASC SEPARATOR " ")
FROM book_mast
GROUP BY pub_id') as $row) {
echo "<tr>";
echo "<td>" . $row['pub_id'] . "</td>";
echo "<td>" . $row['GROUP_CONCAT(DISTINCT cate_id
ORDER BY  cate_id ASC SEPARATOR " ")'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Go Top



<<PreviousNext>>

Looking for some other tutorial?