MySQL | Group_CONCAT()函数
MySQL 中的 GROUP_CONCAT()函数用于将多行的数据连接到一个字段中。这是一个聚合 (GROUP BY)函数,如果组包含至少一个非NULL值,则返回一个字符串值。否则,它返回NULL 。
句法:
SELECT col1, col2, ..., colN
GROUP_CONCAT ( [DISTINCT] col_name1
[ORDER BY clause] [SEPARATOR str_val] )
FROM table_name GROUP BY col_name2;
col1, col2, ...colN : These are the column names of table.
col_name1: Column of the table whose values are concatenated into a single field for each group.
table_name: Name of table.
col_name2: Column of the table according to which grouping is done.
在 GROUP_CONCAT()函数中使用各种子句:
- Distinct:它消除了结果中值的重复。
- Order By:它按特定顺序对组的值进行排序,然后将它们连接起来。
- 分隔符:默认情况下,组的值由 ( , )运算符分隔。为了更改此分隔符值,使用 Separator 子句后跟字符串字面量。它以分隔符 'str_value' 的形式给出。
例子:
让我们考虑一个“员工”表:
emp_id | fname | lname | dept_id | strength |
---|---|---|---|---|
1 | mukesh | gupta | 2 | Leadership |
3 | neelam | sharma | 3 | Hard-working |
1 | mukesh | gupta | 2 | Responsible |
2 | devesh | tyagi | 2 | Punctuality |
3 | neelam | sharma | 3 | Self-motivated |
1 | mukesh | gupta | 2 | Quick-learner |
4 | keshav | singhal | 3 | Listening |
2 | devesh | tyagi | 2 | Quick-learner |
5 | tanya | jain | 1 | Hard-working |
4 | keshav | singhal | 3 | Critical thinking |
5 | tanya | jain | 1 | Goal-oriented |
查询:
1. 使用简单的 GROUP_CONCAT()函数-
SELECT emp_id, fname, lname, dept_id,
GROUP_CONCAT ( strength ) as "strengths"
FROM employee group by emp_id;
输出:emp_id fname lname dept_id strengths 1 mukesh gupta 2 Leadership, Responsible, Quick-learner 2 devesh tyagi 2 Punctuality, Quick-learner 3 neelam sharma 3 Hard-working, Self-motivated 4 keshav singhal 3 Listening, Critical thinking 5 tanya jain 1 Hard-working, Goal-oriented
2. 使用 DISTINCT 子句-
SELECT dept_id,
GROUP_CONCAT ( DISTINCT strength)
as "employees strengths"
from employee group by dept_id;
输出:dept_id employees strengths 1 Goal-oriented, Hard-working 2 Leadership, Punctuality, Quick-learner, Responsible 3 Critical thinking, Hard-working, Listening, Self-motivated
3. ORDER BY 子句的使用:
SELECT dept_id,
GROUP_CONCAT ( DISTINCT emp_id ORDER BY emp_id SEPARATOR', ')
as "employees ids"
from employee group by dept_id;
在这里,分隔符 ', '将用逗号 ( , ) 和空格字符分隔值。
输出:dept_id employees ids 1 5 2 1, 2 3 3, 4
如何在单个字段中连接多行不同的列。
到目前为止,我们已经看到使用 GROUP_CONCAT()函数对属于同一列的多行的值进行分组。但是,结合使用concat()函数和 group_concat()函数,我们可以将不同行的多个列值组合成一个字段。
例子:
考虑到上表“员工”,如果我们希望在第二个查询中找到员工实力和员工 ID,那么它可以写成 -
SELECT dept_id, GROUP_CONCAT ( strengths SEPARATOR ' ') as "emp-id : strengths"
FROM ( SELECT dept_id, CONCAT ( emp_id, ':', GROUP_CONCATt(strength SEPARATOR', ') )
as "strengths" FROM employee GROUP BYy emp_id )as emp GROUP BY dept_id;
解释:
上面的查询由两个 SELECT 语句组成,一个是内部的,一个是外部的。
内部 SELECT 语句 –
SELECT dept_id, concat ( emp_id, ':',
GROUP_CONCAT ( strength separator ', ' ) ) as "strengths"
FROM employee GROUP BY emp_id
它将根据“emp_id”对员工表的行进行分组。结果的第一列显示 dept_id,第二列显示 emp_id 及其优势列表。
内部 SELECT 语句的输出-dept_id strengths 2 1: Leadership, Responsible, Quick-learner 2 2: Punctuality, Quick-learner 3 3: Hard-working, Self-motivated 3 4: Listening, Critical thinking 1 5: Hard-working, Goal-oriented
外部 SELECT 语句现在将根据“dept_id”对这些行进行分组。
输出:dept_id emp-id : strengths 1 5: Hard-working, Goal-oriented 2 1: Leadership, Responsible, Quick-learner 2:Punctuality, Quick-learner 3 3: Hard-working, Self-motivated 4:Listening, Critical thinking
注意: GROUP_CONCAT()函数的结果被截断为系统变量group_concat_max_len给出的最大长度,即1024 。但是,group_concat_max_len 变量的值可以在运行时通过使用SET命令更改为 -
SET [GLOBAL | SESSION] group_concat_max_len = value;
value: It is the new value set to the variable.