关于mysql函数concat与group_concat使用说明事项


本文摘自PHP中文网,作者jacklove,侵删。

1.concat()函数

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

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

例如:

1

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

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

1

2

3

4

mysql> select concat(',',name,',') from `user`;

+--------------------------+| concat(',',fdipzone,',') |

+--------------------------+| ,fdipzone,               |

+--------------------------+1 row in set (0.00 sec)


2.其中有一个参数为null

1

2

3

4

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.使用’,’分隔多个字段

1

2

3

4

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

1

2

3

4

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与空字符串

1

2

3

4

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 “”完全移除这个分隔符。

使用例子:

表结构

1

2

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;

插入数据:

1

2

3

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');

1

2

3

4

5

6

7

8

9

10

11

12

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

1

2

3

4

5

6

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返回值最大长度

1

2

3

4

mysql> show global variables like '%group_concat_max_len%';

+----------------------+-------+| Variable_name        | Value |

+----------------------+-------+| group_concat_max_len | 1024  |

+----------------------+-------+

修改group_concat返回值最大长度

1

2

3

4

5

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使用说明,更多相关内容请关注php中文网。
相关推荐:

关于mysql innodb启动失败无法重启的处理方法讲解

讲解php获取指定日期的相关内容

详解PHP生成唯一RequestID类

以上就是关于mysql函数concat与group_concat使用说明事项的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

mysql的索引和事务详细解读

mysql安装后.net程序运行出错如何解决

mysql的3种分表方案

mysql常用语句及用法

mysql怎么查看有没有死锁

mysql定义异常和异常处理详解

mysql安装图文详解

mysql需要commit么

alisql和mysql区别

mysql教程--通过配置文件连接数据库操作详解

更多相关阅读请进入《mysql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...