MySQL GROUP_CONCAT() function
GROUP_CONCAT() function
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
Contents:
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;
Sample 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

PHP script
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<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>
JSP script
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-group_concat</title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String Host = "jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data = "SELECT pub_id,GROUP_CONCAT(cate_id) FROM book_mast GROUP BY pub_id";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Publisher id</td>
<td>GROUP_CONCAT(cate_id)</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("pub_id")%></TD>
<TD><%=rs.getString("GROUP_CONCAT(cate_id)")%></TD>
</TR>
<% } %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>
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;
Sample 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.0">
<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>
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;
Sample 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.0">
<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>
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 ;
Sample 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.0">
<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>
Online Practice Editor:
Previous:
COUNT(DISTINCT)
Next:
MySQL Aggregate Functions and Grouping - Max()
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook