MySQL function -GROUP_CONCAT MySQL function GROUP_CONCAT

MySQL function GROUP_CONCAT

This function returns the string with a non-NULL value results from the connection of one group. This function is an enhanced Sybase SQL Anywhere supports the basic LIST () function.

Grammatical structures:

 

GROUP_CONCAT([ DISTINCT ] expr [,expr ...] [ ORDER  BY  {unsigned_integer | col_name | expr} [ ASC  | DESC ] [,col_name ...]] [SEPARATOR str_val])

 

DISTINCT: removing duplicate values

expr [, expr ...]: one or more fields (or expressions)

ORDER BY {unsigned_integer | col_name | expr} [ASC | DESC] [, col_name ...]: sorted according field or expression may be a plurality of

SEPARATOR str_val: separator (default comma)

 

Example 1: Query a classification of all sub-categories and sub-categories connection ID with a comma

 

mysql> SELECT  GROUP_CONCAT(cat_id) FROM  goods_cat WHERE  pid = 25
+ -----------------------------+
| GROUP_CONCAT(cat_id)        |
+ -----------------------------+
| 26,111,130,206,239,322,323  |
+ -----------------------------+

 

 

Example 2: Search for a child of sorting and classification ID linker with a semicolon

mysql> SELECT  GROUP_CONCAT(cat_id SEPARATOR ';' ) FROM  goods_cat WHERE  pid = 25
+ -------------------------------------+
| GROUP_CONCAT(cat_id SEPARATOR ';' )  |
+ -------------------------------------+
| 26;111;130;206;239;322;323          |
+ -------------------------------------+

 

Example 3: Query a classification of all sub-categories, according to the connection p_order ASC, cat_id DESC after sorting

mysql> SELECT  GROUP_CONCAT(cat_id ORDER  BY  p_order ASC , cat_id DESC ) FROM  goods_cat WHERE  pid = 25
+ ----------------------------------------------------------+
| GROUP_CONCAT(cat_id ORDER  BY  p_order ASC , cat_id DESC )   |
+ ----------------------------------------------------------+
| 332,331,242,212,133,112,29,26,333,330,327,244,138,116    |
+ ----------------------------------------------------------+

 

Example 4: combined GROUP BY query

mysql> SELECT  pid, GROUP_CONCAT(cat_id) FROM  goods_cat GROUP  BY  pid
+ -----------+-------------------------------------+
| parent_id | GROUP_CONCAT(cat_id)                |
+ -----------+-------------------------------------+
|        22 | 35,166,191,209,233,252,256,257,258  |
|        25 | 26,111,130,206,239,322,323          |
|        26 | 29,51,65,66,70,75,238               |
|       323 | 332,333,334,335,336,337,338,339     |
+ -----------+-------------------------------------+

 

note:

1. Maximum length (characters) limits

System variables: group_concat_max_len

SET  [SESSION | GLOBAL ] group_concat_max_len = val;

val must be an unsigned integer

GROUP_CONCAT with the function, SELECT statement LIMIT statement no role to play.

2. INT type trap

When connected to INT field type, version, or low results are not returned string comma-delimited occurs, but byte [].

In this case, the need for conversion CAST or CONVERT function.

This function returns the string with a non-NULL value results from the connection of one group. This function is an enhanced Sybase SQL Anywhere supports the basic LIST () function.

Grammatical structures:

 

GROUP_CONCAT([ DISTINCT ] expr [,expr ...] [ ORDER  BY  {unsigned_integer | col_name | expr} [ ASC  | DESC ] [,col_name ...]] [SEPARATOR str_val])

 

DISTINCT: removing duplicate values

expr [, expr ...]: one or more fields (or expressions)

ORDER BY {unsigned_integer | col_name | expr} [ASC | DESC] [, col_name ...]: sorted according field or expression may be a plurality of

SEPARATOR str_val: separator (default comma)

 

Example 1: Query a classification of all sub-categories and sub-categories connection ID with a comma

 

mysql> SELECT  GROUP_CONCAT(cat_id) FROM  goods_cat WHERE  pid = 25
+ -----------------------------+
| GROUP_CONCAT(cat_id)        |
+ -----------------------------+
| 26,111,130,206,239,322,323  |
+ -----------------------------+

 

 

Example 2: Search for a child of sorting and classification ID linker with a semicolon

mysql> SELECT  GROUP_CONCAT(cat_id SEPARATOR ';' ) FROM  goods_cat WHERE  pid = 25
+ -------------------------------------+
| GROUP_CONCAT(cat_id SEPARATOR ';' )  |
+ -------------------------------------+
| 26;111;130;206;239;322;323          |
+ -------------------------------------+

 

Example 3: Query a classification of all sub-categories, according to the connection p_order ASC, cat_id DESC after sorting

mysql> SELECT  GROUP_CONCAT(cat_id ORDER  BY  p_order ASC , cat_id DESC ) FROM  goods_cat WHERE  pid = 25
+ ----------------------------------------------------------+
| GROUP_CONCAT(cat_id ORDER  BY  p_order ASC , cat_id DESC )   |
+ ----------------------------------------------------------+
| 332,331,242,212,133,112,29,26,333,330,327,244,138,116    |
+ ----------------------------------------------------------+

 

Example 4: combined GROUP BY query

mysql> SELECT  pid, GROUP_CONCAT(cat_id) FROM  goods_cat GROUP  BY  pid
+ -----------+-------------------------------------+
| parent_id | GROUP_CONCAT(cat_id)                |
+ -----------+-------------------------------------+
|        22 | 35,166,191,209,233,252,256,257,258  |
|        25 | 26,111,130,206,239,322,323          |
|        26 | 29,51,65,66,70,75,238               |
|       323 | 332,333,334,335,336,337,338,339     |
+ -----------+-------------------------------------+

 

note:

1. Maximum length (characters) limits

System variables: group_concat_max_len

SET  [SESSION | GLOBAL ] group_concat_max_len = val;

val must be an unsigned integer

GROUP_CONCAT with the function, SELECT statement LIMIT statement no role to play.

2. INT type trap

When connected to INT field type, version, or low results are not returned string comma-delimited occurs, but byte [].

In this case, the need for conversion CAST or CONVERT function.

Guess you like

Origin www.cnblogs.com/shishibuwan/p/12027830.html