gpt4 book ai didi

mysql group_concat 对多个字段进行分组

转载 作者:行者123 更新时间:2023-11-29 03:56:52 25 4
gpt4 key购买 nike

我有一个表成员 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 |

Here is SQLFiddle demo

On a side note: providing sample data and desired output in a question like this usually improves your changes of getting your answer faster and that best fits your needs.

UPDATE: You can deeply pack information using GROUP_CONCAT() using different separators if it's what you want

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() 轻松地将 details 列展开为单独的记录.

关于mysql group_concat 对多个字段进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18113803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com