- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to combine duplicate values into one with corresponding value separated by hyphens in MySQL?
To combine, use GROUP_CONCAT() function to combine some attributes in two rows into one. As a separator, use hyphens.
Let us first create a table −
mysql> create table DemoTable1386 -> ( -> Title varchar(255), -> Name varchar(60) -> ); Query OK, 0 rows affected (0.67 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1386 values('Introduction to MySQL','Paul DuBois'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1386 values('Java in Depth','Khalid Mughal'); Query OK, 1 row affected (0.48 sec) mysql> insert into DemoTable1386 values('Introduction to MySQL','Russell Dyer'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1386 values('Java in Depth','Joshua Bloch'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1386;
This will produce the following output −
+-----------------------+---------------+ | Title | Name | +-----------------------+---------------+ | Introduction to MySQL | Paul DuBois | | Java in Depth | Khalid Mughal | | Introduction to MySQL | Russell Dyer | | Java in Depth | Joshua Bloch | +-----------------------+---------------+ 4 rows in set (0.00 sec)
Following is the query to combine duplicate values −
mysql> select Title,group_concat(Name separator '----') from DemoTable1386 -> group by Title;
This will produce the following output −
+-----------------------+-------------------------------------+ | Title | group_concat(Name separator '----') | +-----------------------+-------------------------------------+ | Introduction to MySQL | Paul DuBois----Russell Dyer | | Java in Depth | Khalid Mughal----Joshua Bloch | +-----------------------+-------------------------------------+ 2 rows in set (0.00 sec)
- Related Articles
- Display duplicate record as a distinct value with corresponding values as distinct comma separated list in MySQL?
- How to concatenate columns based on corresponding duplicate id values in MySQL? Display the duplicate values in the same column separated by slash
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- Get minimum value from a column (floating values) with corresponding duplicate ids in MySQL
- MySQL query to fetch the maximum corresponding value from duplicate column values
- Add records from corresponding duplicate values in another column with MySQL
- How to concatenate two column values into a single column with MySQL. The resultant column values should be separated by hyphen
- MySQL rows concatenation to fetch maximum corresponding value from duplicate IDs?
- Combine columns into rows with MySQL?
- How to fetch random rows in MySQL with comma separated values?
- Find average on the basis of corresponding duplicate VARCHAR values in MySQL
- SUM corresponding duplicate records in MySQL
- Combine values of two columns separated with hyphen in an R data frame.
- MySQL query to concatenate records with similar corresponding ids in a single row separated by a special character
- MySQL query to search between comma separated values within one field?

Advertisements