数据库
首页 > 数据库> > mysql group_concat group by on multiple fields

mysql group_concat group by on multiple fields

作者:互联网

我有一个表成员与member_id,member_name,club_name,region,zone,email作为字段.

我正在使用MySQL的group_concat函数

SELECT group_concat(distinct m.email
SEPARATOR ', ' ) from member m group by m.club_name

这工作正常.但我希望能够在其他字段上进行group_concat而无需创建其他查询.

是否可以提供其他字段作为参数?

member_id   member_name club_name   region  zone    email
1           member1           A        1    1   email1@example.com
2           member2           A        1    1   email2@example.com
3           member3           B        1    1   email3@example.com
4           member4           C        1    2   email4@example.com
5           member5           D        2    1   email5@example.com

**group by club**
email1@example.com,email2@example.com
email3@example.com
email4@example.com
email5@example.com

**group by region**
email1@example.com, email2@example.com, email3@example.com, email4@example.com
email5@example.com

**group by zone**
email1@example.com, email2@example.com, email3@example.com
email5@example.com

假设每个地区都有3个区域,每个区域都有不止一个俱乐部.现在,我如何获得可以分组或与地区,区域或俱乐部相关的电子邮件?

解决方法:

完全从你的问题中了解你是什么,但你可以尝试

SELECT club_name,
       GROUP_CONCAT(DISTINCT email SEPARATOR ', ' ) emails,
       GROUP_CONCAT(DISTINCT member_name SEPARATOR ', ' ) members
       ... 
 FROM member
GROUP BY club_name

样本输出:

| CLUB_NAME |                                EMAILS |          MEMBERS |
------------------------------------------------------------------------
|     Club1 | m1@mail.com, m2@mail.com, m3@mail.com | Jhon, Mark, Beth |
|     Club2 |              m4@mail.com, m5@mail.com |    Helen, Thomas |

这是SQLFiddle演示

请注意:在这样的问题中提供样本数据和所需的输出通常会改善您更快地获得答案并最符合您需求的变化.

更新:您可以使用GROUP_CONCAT()使用不同的分隔符深度打包信息(如果它是您想要的)

SELECT 'club' group_type, GROUP_CONCAT(details SEPARATOR '|') details
  FROM
(
  SELECT CONCAT(club_name, ';', GROUP_CONCAT(DISTINCT email)) details
    FROM member
   GROUP BY club_name
) a
UNION ALL
SELECT 'region' group_type, GROUP_CONCAT(details SEPARATOR '|') details
  FROM
(
  SELECT CONCAT(region, ';', GROUP_CONCAT(DISTINCT email)) details
    FROM member
   GROUP BY region
) a
UNION ALL
SELECT 'zone' group_type, GROUP_CONCAT(details SEPARATOR '|') details
  FROM
(
  SELECT CONCAT(zone, ';', GROUP_CONCAT(DISTINCT email)) details
    FROM member
   GROUP BY zone
) a

样本输出:

| GROUP_TYPE |                                                                                                DETAILS |
-----------------------------------------------------------------------------------------------------------------------
|       club | A;email1@example.com,email2@example.com|B;email3@example.com|C;email4@example.com|D;email5@example.com |
|     region |     1;email1@example.com,email2@example.com,email3@example.com,email4@example.com|2;email5@example.com |
|       zone |     1;email1@example.com,email2@example.com,email3@example.com,email5@example.com|2;email4@example.com |

这是SQLFiddle演示

如果您在客户端使用php,则可以在迭代结果集时使用explode()轻松地将详细信息列展开到单独的记录中.

标签:mysql,group-concat
来源: https://codeday.me/bug/20191006/1858737.html