當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


MySQL Group_CONCAT()用法及代碼示例


MySQL中的GROUP_CONCAT()函數用於將多行數據合並為一個字段。這是一個匯總(GROUP BY)函數,如果該組包含至少一個非NULL值,則該函數返回String值。否則,它返回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()函數內部使用各種子句:


  • Dinstinct:它消除了結果中值的重複。
  • Order By:它按特定順序對組的值進行排序,然後將它們連接起來。
  • Separator:默認情況下,group的值由(,)運算符分隔。為了更改此分隔符值,請使用Separator子句,後跟字符串文字。它以分隔符“ str_value”的形式給出。

例子:

讓我們考慮一個“Employee”表:

emp_id 名字 dept_id 強度
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 名字 dept_id 長處
    1 mukesh gupta 2 Leadership, Resposible, 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 員工優勢
    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 員工編號
    1 5
    2 1, 2
    3 3, 4

如何在單個字段中連接不同列的多行。

到現在為止,我們已經看到使用GROUP_CONCAT()函數將屬於同一列的多行的值進行分組。但是,結合使用concat()函數和group_concat()函數,我們可以將不止行的多個列值組合到單個字段中。

例:

考慮到上表“employee”,如果我們希望在第二個查詢中找到員工實力和員工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”對employee表的行進行分組。結果的第一列顯示dept_id,第二列顯示emp_id及其強度列表。

輸出對於內部SELECT語句-

dept_id 長處
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:優點
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()函數的結果被截斷為最大長度,即1024,這由係統變量group_concat_max_len給出。但是,可以在運行時通過使用SETcommand as-來更改group_concat_max_len變量的值-

SET [GLOBAL | SESSION] group_concat_max_len = value;

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


相關用法


注:本文由純淨天空篩選整理自Tanvi_Garg大神的英文原創作品 MySQL | Group_CONCAT() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。