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)