MYSQL 如何将具有相同字段 ID 的行合并为一行

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

我有一个 select 语句,其中的行为 table_Schema

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

我需要合并行,以便将具有相同行 ID 的博客合并为一行。 我正在尝试获取 <author fname, author mname, author lname | coauthor fname coauthor lname> 或作为作者共同作者的专栏 每个博客可以有多位作者和合著者为例 Blog1 的作者是 Thomas Edison、Dan Mathre、Robert Cook,Joe Randall 是合著者。

我试过了

CONCAT_WS('|', group_concat(concat(ifnull(people.prefix, ' '), ' ' ,ifnull(people.first_name, ' '), ' ' ,ifnull(people.middle_name, ' '), ' ' ,ifnull(people.last_name, ' '), '|', ifnull(peopleData.prefix, ' '), ' ' ,ifnull(peopleData.first_name, ' '), ' ' ,ifnull(peopleData.middle_name, ' '), ' ' ,ifnull(peopleData.last_name, ' '))) ) 
             AS authors

理想情况下 return 作者姓名和共同作者姓名由 ||| 分隔。

然而这里有多次迭代

Roy Thomas | Joe Shan, Roy Thomas | Kunal Shar, Thomas Edison | Joe Shan, Thomas Edison | Kunal Shar

GROUP_CONCAT 支持 DISTINCT 和 SEPARATOR``

CREATE TABLE table1 (
  `rowid` VARCHAR(139),
  `title` VARCHAR(139),
  `author_f_name` VARCHAR(139),
  `author_m_name` VARCHAR(139),
  `author_l_name` VARCHAR(139),
  `coauthor_first_name` VARCHAR(139),
  `coauthor_middle_name` VARCHAR(139),
  `coauthor_last_name` VARCHAR(139)
);

INSERT INTO table1
  (`rowid`, `title`, `author_f_name`, `author_m_name`, `author_l_name`, `coauthor_first_name`, `coauthor_middle_name`, `coauthor_last_name`)
VALUES
  ('1.',   'Blog Title.',       'Roy',                  NULL,                   'Thomas.',               'Joe',                   'Shann',               'Mathews'),
  ('1.',   'Blog Title.',       'Thomas',                  'NULL',              'Edison',            'Kunal',               NULL,                       'Shar');
SELECT 
`rowid`
, GROUP_CONCAT(DISTINCT `title`  SEPARATOR ' |||') tilte
, GROUP_CONCAT(DISTINCT `author_f_name` SEPARATOR ' |||') author_f_name
, GROUP_CONCAT(DISTINCT `author_m_name` SEPARATOR ' |||') author_m_name
, GROUP_CONCAT(DISTINCT `author_l_name` SEPARATOR ' |||') author_l_name
, GROUP_CONCAT(DISTINCT `coauthor_first_name` SEPARATOR ' |||') coauthor_first_name
, GROUP_CONCAT(DISTINCT `coauthor_middle_name` SEPARATOR ' |||') coauthor_middle_name
, GROUP_CONCAT(DISTINCT `coauthor_last_name` SEPARATOR ' |||') coauthor_last_name
FROM table1
GROUP BY `rowid`
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

SELECT 
`rowid`
, GROUP_CONCAT(DISTINCT `title`  SEPARATOR ' |||') tilte
, GROUP_CONCAT(DISTINCT CONCAT(`author_f_name`,' ',COALESCE(`author_m_name`,''),' ',`author_l_name`) SEPARATOR ' |||') author_full_name
, GROUP_CONCAT(DISTINCT CONCAT(`coauthor_first_name`,' ',COALESCE(`coauthor_middle_name`,''),' ',`coauthor_last_name`) SEPARATOR ' |||') coauthor_full_name
FROM table1
GROUP BY `rowid`
rowid | tilte       | author_full_name                   | coauthor_full_name              
:---- | :---------- | :--------------------------------- | :-------------------------------
1.    | Blog Title. | Roy  Thomas. |||Thomas NULL Edison | Joe Shann Mathews |||Kunal  Shar

db<>fiddle here