MySQL 连接字符串函数 CONCAT() CONCAT_WS() GROUP_CONCAT()

默北 MySQLMySQL 连接字符串函数 CONCAT() CONCAT_WS() GROUP_CONCAT()已关闭评论19,7761字数 781阅读2分36秒阅读模式

最近遇到一些蛋疼的事情, 所有的业务处理全交给MySQL函数来处理, 导致mysql CPU使用率骑虎难下。SQL语音也是一门语言,也包括一些连接字符串的处理函数,如:  CONCAT()、  CONCAT_WS()、  GROUP_CONCAT() 。下面说说这几个函数的使用。

1. CONCAT()文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

语法:CONCAT(str1,str2,...)文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

可以有一个或多个参数,返回的结果为连接参数产生的字符串。文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

如果所有参数均为非二进制字符串,则结果为非二进制字符串。文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

如果参数含有任何一个二进制字符串,则结果为一个二进制字符串。文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

数字参数会转化为与之相等的二进制字符串,如果希望避免这种情况,可以使用显式类型 cast。如:SELECT CONCAT(CAST(int_col AS CHAR), char_col);文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

如果参数含有NULL,则结果为NULL。文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

concat文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

2.  CONCAT_WS()文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

语法:CONCAT_WS(separator,str1,str2,...)文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

第一个参数是其它参数的分隔符。分隔符的位置在连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。如果分隔符是NULL,则结果为NULL。如果其他参数含有NULL,则会被忽略该NULL。文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

concat文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

3.  GROUP_CONCAT()文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

语法:文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col_name ...]]
[SEPARATOR str_val])文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

默认以','分隔。文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

concat文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

转载请注明来自运维生存时间:  https://www.ttlsa.com/html/3778.html文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/ 文章源自运维生存时间-https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 25/11/2013 10:14:18
  • 转载请务必保留本文链接:https://www.ttlsa.com/mysql/the-mysql-connection-string-function-concat-concat_ws-group_concat/
  • concat
  • CONCAT_WS()
  • GROUP_CONCAT()
  • mysql
  • 连接字符串函数