concat与group_concat函数使用流程

2023-06-09,,

本文主要给大家介绍concatgroup_concat函数使用流程,其所涉及的东西,从理论知识来获悉,有很多书籍、文献可供大家参考,从现实意义角度出发,本站累计多年的实践经验可分享给大家。

1.concat()函数

使用方法:concat(str1,str2,…)
返回的结果为连接参数产生的字符串,如有任何一个参数为null,则返回值为null

注意:
如果所有参数均为非二进制字符串,则结果为非二进制字符串
如果自变量中含有任一二进制字符串,则结果为二进制字符串
一个数字参数被转为与之相等的二进制字符串格式,如要避免这种情况,可使用显式类型cast

例如:

select concat(cast(int_col as char), char_col);

使用例子:
1.字段两端加上’,’

mysql> select concat(',',name,',') from `user`;
+--------------------------+| concat(',',fdipzone,',') |
+--------------------------+| ,fdipzone,               |
+--------------------------+1 row in set (0.00 sec)

2.其中有一个参数为null

mysql> select concat(null,name) from `user`;
+-------------------+| concat(null,name) |
+-------------------+| NULL              |
+-------------------+1 row in set (0.00 sec)

2.concat_ws() 函数

使用方法:concat_ws(separator,str1,str2,…)

concat_ws()函数是concat()函数的特殊形式,第一个参数是其他参数的分隔符。分隔符的位置在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其他参数。
如果分隔符为null,则结果为null。
函数会忽略任何分隔符参数后的null值,但concat_ws()不会忽略任何空字符串。

使用例子:
1.使用’,’分隔多个字段

mysql> select concat_ws(',',country_code,phone,region) from `user`;
+------------------------------------------+| concat_ws(',',country_code,phone,region) |
+------------------------------------------+| 86,13794830550,GZ                        |
+------------------------------------------+1 row in set (0.00 sec)

2.分隔符为null

mysql> select concat_ws(null,country_code,phone,region) from `user`;
+-------------------------------------------+| concat_ws(null,country_code,phone,region) |
+-------------------------------------------+| NULL                                      |
+-------------------------------------------+1 row in set (0.00 sec)

3.参数中有null与空字符串

mysql> select concat_ws(',',country_code,phone,null,region,'',grade) from `user`;
+--------------------------------------------------------+| concat_ws(',',country_code,phone,null,region,'',grade) |
+--------------------------------------------------------+| 86,13794830550,GZ,,200                                 |
+--------------------------------------------------------+1 row in set (0.00 sec)

3.group_concat()函数

使用方法:GROUP_CONCAT([DISTINCT] expr [,expr …]
[ORDER BY {unsigned_integer | col_name | formula} [ASC | DESC] [,col …]]
[SEPARATOR str_val])

group_concat可以得到表达式结合体的连结值,使用distinct可以排除重复值。使用order by子句可以排序。
separator是一个字符串,用于分隔结果集中每个元素。默认是逗号,可以通过指定separator “”完全移除这个分隔符。

使用例子:

表结构

CREATE TABLE `article_in_category` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `article_id` int(11) unsigned NOT NULL, `category_id` int(11) unsigned NOT NULL, PRIMARY KEY (`id`), KEY `article_id_INDEX` (`article_id`), KEY `category_id_INDEX` (`category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

插入数据:

INSERT INTO `article_in_category` (`id`, `article_id`, `category_id`) VALUES (NULL, '1', '1'),
(NULL, '1', '2'),(NULL, '1', '3'),(NULL, '2', '4'),(NULL, '2', '3'),(NULL, '2', '5'),(NULL, '3', '1'),
(NULL, '3', '5'),(NULL, '3', '6'),(NULL, '4', '8');
mysql> select * from `article_in_category`;
+----+------------+-------------+| id | article_id | category_id |
+----+------------+-------------+|  1 |          1 |           1 |
|  2 |          1 |           2 |
|  3 |          1 |           3 |
|  4 |          2 |           4 |
|  5 |          2 |           3 |
|  6 |          2 |           5 |
|  7 |          3 |           1 |
|  8 |          3 |           5 |
|  9 |          3 |           6 || 10 |          4 |           8 |
+----+------------+-------------+

获取文章的id及所有分类id

mysql> select article_id,group_concat(category_id order by category_id asc) from `article_in_category` group by article_id;
+------------+----------------------------------------------------+| article_id | group_concat(category_id order by category_id asc) |
+------------+----------------------------------------------------+|          1 | 1,2,3                                              |
|          2 | 3,4,5                                              |
|          3 | 1,5,6                                              ||          4 | 8                                                  |
+------------+----------------------------------------------------+4 rows in set (0.00 sec)

注意:group_concat()函数对返回的结果有长度限制,默认为1024字节

查看group_concat返回值最大长度

mysql> show global variables like '%group_concat_max_len%';
+----------------------+-------+| Variable_name        | Value |
+----------------------+-------+| group_concat_max_len | 1024  |
+----------------------+-------+

修改group_concat返回值最大长度

mysql> set global group_concat_max_len=2048;
Query OK, 0 rows affected (0.03 sec)mysql> show global variables like '%group_concat_max_len%';
+----------------------+-------+| Variable_name        | Value |
+----------------------+-------+| group_concat_max_len | 2048  |
+----------------------+-------+

以上就是关于mysql函数concat与group_concat使用说明事项的详细内容,更多请关注本站其它相关文章!

《concat与group_concat函数使用流程.doc》

下载本文的Word格式文档,以方便收藏与打印。