
- 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
How to concatenate fields in MySQL?
To concatenate fields in MySQL, you can use GROUP_CONCAT() along with GROUP BY. Let us first create a table −
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(30), StudentScore int ); Query OK, 0 rows affected (0.51 sec)
Insert records in the table using insert command −
mysql> insert into DemoTable( StudentName,StudentScore) values('Bob',80); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable( StudentName,StudentScore) values('Bob',80); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable( StudentName,StudentScore) values('Chris',90); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable( StudentName,StudentScore) values('Chris',70); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable( StudentName,StudentScore) values('Bob',50); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable( StudentName,StudentScore) values('David',60); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable( StudentName,StudentScore) values('Chris',99); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable( StudentName,StudentScore) values('David',88); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable;
This will produce the following output −
+-----------+-------------+--------------+ | StudentId | StudentName | StudentScore | +-----------+-------------+--------------+ | 1 | Bob | 80 | | 2 | Bob | 80 | | 3 | Chris | 90 | | 4 | Chris | 70 | | 5 | Bob | 50 | | 6 | David | 60 | | 7 | Chris | 99 | | 8 | David | 88 | +-----------+-------------+--------------+ 8 rows in set (0.00 sec)
Following is the query to concatenate fields in MySQL −
mysql> select StudentName, group_concat(StudentScore separator ',') as Score from DemoTable group by StudentName;
This will produce the following output −
+-------------+----------+ | StudentName | Score | +-------------+----------+ | Bob | 80,80,50 | | Chris | 90,70,99 | | David | 60,88 | +-------------+----------+ 3 rows in set (0.24 sec)
- Related Articles
- Concatenate fields in MongoDB?
- How to concatenate more than 2 fields with SQL?
- How to concatenate all columns in MySQL?
- How to mask data fields in MySQL?
- MongoDB query to concatenate values of array with other fields
- How to ORDER BY grouped fields in MySQL?
- How to display the bit(1) fields in MySQL?
- How to select rows with condition via concatenate in MySQL?
- Concatenate strings from two fields into a third field in MongoDB?
- Concatenate two columns in MySQL?
- How to concatenate all values of a single column in MySQL?
- How to concatenate MySQL distinct query results into a string?
- Concatenate string with numbers in MySQL?
- Substring() for Fields in MySQL?
- How to apply Substring() for fields in MySQL to get part of string?

Advertisements