目录
1. CONCAT()
2. CONCAT_WS()
3. GROUP_CONCAT()
1. CONCAT()
API:
CONCAT(str1,str2,...)
Desc:
Example1:
SELECT
CONCAT( t.id, ',', t.NAME, ',', t.score ) AS info
FROM
tt2 t
2. CONCAT_WS()
API:
CONCAT_WS(separator,str1,str2,...)
Desc:
Example1:
SELECT
CONCAT_WS(',', t.id, t.NAME, t.score ) AS info
FROM
tt2 t
Example2:
SELECT
CONCAT_WS(NULL, t.id, t.NAME, t.score ) AS info
FROM
tt2 t
3. GROUP_CONCAT()
This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values. The full syntax is as follows:
API:
GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col_name ...]]
[SEPARATOR str_val])
Desc:
SET [GLOBAL | SESSION] group_concat_max_len = val;
Example1:
SELECT
t.NAME,
GROUP_CONCAT( t.SUBJECT, t.score ORDER BY score ) AS info
FROM
`tt2` t
GROUP BY
t.NAME
Example2:
SELECT
t.NAME,
GROUP_CONCAT( CONCAT_WS( ":", t.SUBJECT, t.score ) ORDER BY score SEPARATOR "; " ) AS info
FROM
`tt2` t
GROUP BY
t.NAME
Example3:
SET SESSION group_concat_max_len = 999999;
参考:
concat(): https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat concat_ws(): https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat group_concat(): https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_group-concat