简体   繁体   English

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

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

I have a select statement with rows as table_Schema我有一个 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

I need to merge the rows such that blogs with same row id are merged into a single row.我需要合并行,以便将具有相同行 ID 的博客合并为一行。 I am trying to get either <author fname, author mname, author lname | coauthor fname coauthor lname>我正在尝试获取<author fname, author mname, author lname | coauthor fname coauthor lname> <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. <author fname, author mname, author lname | coauthor fname coauthor lname>或一个列作为作者共同作者 每个博客可以有多个作者和共同作者,例如 Blog1 有 Thomas Edison、Dan Mathre、Robert Cook 作为作者,Joe Randall 作为共同作者。

I tried我试过了

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

which should ideally return author names and co author names separated by |||.理想情况下应该返回由 ||| 分隔的作者姓名和共同作者姓名。

However there are multiple iterations here as in但是这里有多次迭代,如

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

GROUP_CONCAT supports DISTINCT and SEPARATOR`` 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_first_name | coauthor_middle_name | coauthor_middle_name | coauthor_last_name:---- |:---------- |:------------ |:------------ |:---------------- |:------------------ |:------------------- |:----------------- 1. | coauthor_last_name:---- |:---------- |:------------ |:------------ |:-- -------------- |:---------------- |:------------ ----- |:---------------- 1. | Blog Title.博客标题。 | | Roy |||Thomas |罗伊 |||托马斯 | NULL | NULL | Edison |||Thomas.爱迪生|||托马斯。 | | Joe |||Kunal |乔|||库纳尔| Shann |香 | Mathews |||Shar马修斯 |||沙尔

db<>fiddle here db<> 在这里摆弄

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. | coauthor_full_name:---- |:------------ |:---------------------------- --- |:-------------------------------- 1. | Blog Title.博客标题。 | | Roy Thomas.罗伊·托马斯。 |||Thomas NULL Edison | |||托马斯 NULL 爱迪生 | Joe Shann Mathews |||Kunal Shar乔·山恩·马修斯 |||Kunal Shar

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM