
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Concatenate data from multiple rows using GROUP_CONCAT() in MySQL?
Let us first create a table −
mysql> create table DemoTable (CountryName varchar(100)); Query OK, 0 rows affected (1.01 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('US'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('AUS'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('UK'); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+ | CountryName | +-------------+ | US | | AUS | | UK | +-------------+ 3 rows in set (0.00 sec)
Here is the query to concat multiple rows. We are creating a procedure −
mysql> DELIMITER // mysql> CREATE PROCEDURE searchDemo(in stringValue varchar(255), out output text) BEGIN select group_concat(distinct CountryName order by CountryName) into output from DemoTable where CountryName like stringValue; END // Query OK, 0 rows affected (0.18 sec) mysql> DELIMITER ;
Call the stored procedure with the help of call command −
mysql> call searchDemo('U%',@output); Query OK, 1 row affected, 2 warnings (0.04 sec)
Let us check the value of variable @output −
mysql> select @output;
This will produce the following output −
+---------+ | @output | +---------+ | UK,US | +---------+ 1 row in set (0.00 sec)
- Related Articles
- Concatenate multiple rows and columns in a single row with MySQL
- Getting last value in MySQL group concat?
- Count(*) rows from multiple tables in MySQL?
- Display multiple selected rows using MySQL IN()
- How to concatenate strings using both GROUP_CONCAT() and CONCAT() in the same MySQL query?
- MySQL query to group concat distinct by Id?
- How to remove multiple rows from an R data frame using dplyr package?
- How to create JSON format with group-concat in MySQL?
- Inserting multiple rows in MySQL?
- Using GROUP BY and MAX on multiple columns in MySQL?
- Insert multiple data using SET clause in MySQL?
- How to update multiple rows using single WHERE clause in MySQL?
- Get rows with GROUP BY in MySQL?
- How can we delete multiple rows from a MySQL table?
- How to select rows with condition via concatenate in MySQL?

Advertisements