mysql函数concat与group_concat使用说明

本文介绍了MySQL中三种常用的字符串拼接函数:concat(), concat_ws() 和 group_concat() 的使用方法及注意事项。通过实例展示了如何利用这些函数进行字符串的拼接操作,并解释了group_concat()函数在聚合查询中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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  |
+----------------------+-------+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值