concat(),concat_ws()与group_concat()函数

  1. concat()用于将多个字符拼接起来,CONCAT(str1,str2,…)
SQL> SELECT CONCAT('FIRST ', 'SECOND');
+----------------------------+
| CONCAT('FIRST ', 'SECOND') |
+----------------------------+
| FIRST SECOND               |
+----------------------------+
1 row in set (0.00 sec)

如果有某个字符为空的情况结果为空

SQL> SELECT CONCAT('FIRST ', NULL, 'SECOND');
+----------------------------+
| CONCAT('FIRST ', 'SECOND') |
+----------------------------+
| NULL                       |
+----------------------------+
1 row in set (0.00 sec)
  1. concat_ws()函数用于在拼接的时候指定分隔符,CONCAT_WS(separator,str1,str2,…)
SQL> SELECT CONCAT_WS(',','1','2','3','4');
+--------------------------------+
| CONCAT_WS(',','1','2','3','4') |
+--------------------------------+
| 1,2,3,4                        |
+--------------------------------+
1 row in set (0.00 sec)

如果有某个字符为空的情况结果不为空

SQL> SELECT CONCAT_WS(',','1','2', NULL, '3','4');
+---------------------------------------+
| CONCAT_WS(',','1','2', NULL, '3','4') |
+---------------------------------------+
| 1,2,3,4                               |
+---------------------------------------+
1 row in set (0.00 sec)
  1. group_concat()函数返回一个字符串结果,该结果由分组中的值连接而成,GROUP_CONCAT(expr)
    3.1 使用方法:
GROUP_CONCAT([DISTINCT] expr [,expr ...] [ORDER BY {unsigned_integer | col_name | formula} [ASC | DESC] [,col ...]]
[SEPARATOR str_val])

通过使用DISTINCT排除重复值,使用ORDER BY进行排序,SEPARATOR是一个字符串,用作结果集的分隔符,缺省值为逗号,可以通过指定SEPARATOR “”去掉分隔符。
可以通过group_concat_max_len设置最大长度,如果最大长度被限制,结果集将被剪切到这个最大长度:
SET [SESSION | GLOBAL] group_concat_max_len = unsigned_integer;
3.2 使用info表为例

+----------+----+--------------------------+
| locus    | id | journal                  |
+----------+----+--------------------------+
| AB086827 |  1 | Unpublished              |
| AB086827 |  2 | Submitted (20-JUN-2002)  |
| AF040764 | 23 | Unpublished              |
| AF040764 | 24 | Submitted (31-DEC-1997)  |
+----------+----+--------------------------+

在select下使用直接group_concat结果是先拼接再加分隔符:

SQL> SELECT GROUP_CONCAT(id,locus) FROM info;
+------------------------------------------------------+
| GROUP_CONCAT(id,locus)                               |
+------------------------------------------------------+
| 1AB086827,2AB086827,3AF040764,4AF040764              |
+------------------------------------------------------+
1 row in set (0.00 sec)

在select下使用group by后的group_concat结果是先分组在拼接的:

SQL> SELECT locus,GROUP_CONCAT(id) FROM info WHERE locus IN('AB086827','AF040764') GROUP BY locus;
+----------+------------------+
| locus    | GROUP_CONCAT(id) |
+----------+------------------+
| AB086827 | 1,2              |
| AF040764 | 23,24            |
+----------+------------------+
1 row in set (0.00 sec)

使用distinct过滤重复值,使用order by排序,使用separator重新设置分隔符:

SQL> SELECT locus,GROUP_CONCAT(distinct id ORDER BY id DESC SEPARATOR '_') FROM info WHERE locus IN('AB086827','AF040764') GROUP BY locus;
+----------+----------------------------------------------------------+
| locus    | GROUP_CONCAT(distinct id ORDER BY id DESC SEPARATOR '_') |
+----------+----------------------------------------------------------+
| AB086827 | 2_1                                                      |
| AF040764 | 24_23                                                    |
+----------+----------------------------------------------------------+
1 row in set (0.00 sec)

组合使用group_concat与concat_ws:

SQL> SELECT locus,GROUP_CONCAT(concat_ws(', ',id,journal) ORDER BY id DESC SEPARATOR '. ') FROM info WHERE locus IN('AB086827','AF040764') GROUP BY locus;
+----------+--------------------------------------------------------------------------+
| locus    | GROUP_CONCAT(concat_ws(', ',id,journal) ORDER BY id DESC SEPARATOR '. ') |
+----------+--------------------------------------------------------------------------+
| AB086827 | 2, Submitted (20-JUN-2002). 1, Unpublished                               |
| AF040764 | 24, Submitted (31-DEC-1997) . 23, Unpublished                            |
+----------+--------------------------------------------------------------------------+
1 row in set (0.00 sec)

你可能感兴趣的:(mysql,sql)