前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【MySQL】:CONCAT()、CONCAT_WS()、GROUP_CONCAT() 函数

【MySQL】:CONCAT()、CONCAT_WS()、GROUP_CONCAT() 函数

作者头像
WEBJ2EE
发布2021-09-24 14:53:41
1.9K0
发布2021-09-24 14:53:41
举报
文章被收录于专栏:WebJ2EEWebJ2EE
代码语言:javascript
复制
目录
1. CONCAT() 
2. CONCAT_WS()
3. GROUP_CONCAT()

1. CONCAT()

API:

代码语言:javascript
复制
CONCAT(str1,str2,...)

Desc:

  • Returns the string that results from concatenating the arguments. May have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string. A numeric argument is converted to its equivalent nonbinary string form.
    • CONCAT() returns NULL if any argument is NULL.

Example1:

代码语言:javascript
复制
SELECT
  CONCAT( t.id, ',', t.NAME, ',', t.score ) AS info 
FROM
  tt2 t

2. CONCAT_WS()

API:

代码语言:javascript
复制
CONCAT_WS(separator,str1,str2,...)

Desc:

  • CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT(). The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments.
    • If the separator is NULL, the result is NULL.
    • CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.

Example1:

代码语言:javascript
复制
SELECT
  CONCAT_WS(',', t.id, t.NAME, t.score ) AS info 
FROM
  tt2 t

Example2:

代码语言:javascript
复制
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:

代码语言:javascript
复制
GROUP_CONCAT([DISTINCT] expr [,expr ...]
             [ORDER BY {unsigned_integer | col_name | expr}
                 [ASC | DESC] [,col_name ...]]
             [SEPARATOR str_val])

Desc:

  • In MySQL, you can get the concatenated values of expression combinations. To eliminate duplicate values, use the DISTINCT clause. To sort values in the result, use the ORDER BY clause. To sort in reverse order, add the DESC (descending) keyword to the name of the column you are sorting by in the ORDER BY clause. The default is ascending order; this may be specified explicitly using the ASC keyword. The default separator between values in a group is comma (,). To specify a separator explicitly, use SEPARATOR followed by the string literal value that should be inserted between group values. To eliminate the separator altogether, specify SEPARATOR ''.
    • The result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet. The syntax to change the value of group_concat_max_len at runtime is as follows, where val is an unsigned integer:
代码语言:javascript
复制
SET [GLOBAL | SESSION] group_concat_max_len = val;

Example1:

代码语言:javascript
复制
SELECT
  t.NAME,
  GROUP_CONCAT( t.SUBJECT, t.score ORDER BY score ) AS info 
FROM
  `tt2` t 
GROUP BY
  t.NAME

Example2:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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


本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-09-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 WebJ2EE 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档