Skip to content
Advertisement

MYSQL how to merge rows with same field id into a single row

I have a select statement with rows as table_Schema

I need to merge the rows such that blogs with same row id are merged into a single row. I am trying to get either <author fname, author mname, author lname | coauthor fname coauthor lname> or a column as author co author Each blog can have multiple authors and co authors as an example Blog1 has Thomas Edison, Dan Mathre, Robert Cook as authors and Joe Randall as co author.

I tried

which should ideally return author names and co author names separated by |||.

However there are multiple iterations here as in

Advertisement

Answer

GROUP_CONCAT supports DISTINCT and SEPARATOR“

rowid | tilte       | author_f_name | author_m_name | author_l_name     | coauthor_first_name | coauthor_middle_name | coauthor_last_name
:---- | :---------- | :------------ | :------------ | :---------------- | :------------------ | :------------------- | :-----------------
1.    | Blog Title. | Roy |||Thomas | NULL          | Edison |||Thomas. | Joe |||Kunal        | Shann                | Mathews |||Shar   

db<>fiddle here

rowid | tilte       | author_full_name                   | coauthor_full_name              
:---- | :---------- | :--------------------------------- | :-------------------------------
1.    | Blog Title. | Roy  Thomas. |||Thomas NULL Edison | Joe Shann Mathews |||Kunal  Shar

db<>fiddle here

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement