mysql--group_concat()

34 篇文章 1 订阅

转自:https://www.geeksforgeeks.org/mysql-group_concat-function/

 

一、GROUP_CONCAT()函数
GROUP_CONCAT函数返回一个字符串结果,该结果由分组中的值连接组合而成。

1、使用语法及特点:

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.

Separator: By default, the values of group are separated by (, ) operator. In order to change this separator value, Separator clause is used followed by a string literal. It is given as Separator ‘str_value’.

例1:Let, consider an “Employee” table:

原表如下:

emp_idfnamelnamedept_idstrength
1mukeshgupta2Leadership
3neelamsharma3Hard-working
1mukeshgupta2Responsible
2deveshtyagi2Punctuality
3neelamsharma3Self-motivated
1mukeshgupta2Quick-learner
4keshavsinghal3Listening
2deveshtyagi2Quick-learner
5tanyajain1Hard-working
4keshavsinghal3Critical thinking
5tanyajain1Goal-oriented
SELECT emp_id, fname, lname, dept_id, 
GROUP_CONCAT ( strength ) as "strengths" 
FROM employee group by emp_id;

 

emp_idfnamelnamedept_idstrengths
1mukeshgupta2Leadership, Resposible, Quick-learner
2deveshtyagi2Punctuality, Quick-learner
3neelamsharma3Hard-working, Self-motivated
4keshavsinghal3Listening, Critical thinking
5tanyajain1Hard-working, Goal-oriented

 例2:sing a DISTINCT clause-

SELECT dept_id, 
GROUP_CONCAT ( DISTINCT strength) 
as "employees strengths"  
from employee group by dept_id;
dept_idemployees strengths
1Goal-oriented, Hard-working
2Leadership, Punctuality, Quick-learner, Responsible
3Critical thinking, Hard-working, Listening, Self-motivated

例3:Use of ORDER BY clause: 

SELECT dept_id, 
GROUP_CONCAT ( DISTINCT emp_id ORDER BY emp_id  SEPARATOR', ') 
as "employees ids" 
from employee group by dept_id;

Here, Separator ', ' will separate the values by a comma () and a whitespace character.

Output:

dept_idemployees ids
15
21, 2
33, 4

三、连接单个字段中不同列的多行

How to concatenate multiple rows of different columns in a single field.

Til now we have seen the use of GROUP_CONCAT() function to group the values of multiple rows that belongs to same column. But, using concat() function and group_concat() function together, we can combine more than one column values of different rows into single field.

 

Considering above table “employee”, if we wish to find employees strength along with employees id in second query then it is written as-

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;

The above query consist of two SELECT statements an inner one and the outer one.

The inner SELECT statement –

SELECT dept_id, concat ( emp_id, ':',
GROUP_CONCAT ( strength separator ', ' ) ) as "strengths"  
FROM employee GROUP BY emp_id

Output for inner SELECT statement-

dept_idstrengths
21: Leadership, Responsible, Quick-learner
22: Punctuality, Quick-learner
33: Hard-working, Self-motivated
34: Listening, Critical thinking
15: Hard-working, Goal-oriented

The outer SELECT statement will now group these rows according to “dept_id”.

Output:

dept_idemp-id : strengths
15: Hard-working, Goal-oriented
21: Leadership, Responsible, Quick-learner 2:Punctuality, Quick-learner
33: Hard-working, Self-motivated 4:Listening, Critical thinking

Note: The result of GROUP_CONCAT() function is truncated to the maximum length i.e 1024 which is given by sytem variable group_concat_max_len. However, the value of group_concat_max_len variable can be changed at runtime by using SETcommand as-

SET [GLOBAL | SESSION] group_concat_max_len = value;

value: It is the new value set to the variable.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值