数据准备
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`account` varchar(100) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
`type` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `account` (`account`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `user` (`id`, `account`, `password`, `type`) values('1','zhangsan','123','1');
insert into `user` (`id`, `account`, `password`, `type`) values('2','lisi','123','2');
insert into `user` (`id`, `account`, `password`, `type`) values('3','wangwu','123','3');
insert into `user` (`id`, `account`, `password`, `type`) values('4','kety','123','4');
insert into `user` (`id`, `account`, `password`, `type`) values('5','marh','123','1');
insert into `user` (`id`, `account`, `password`, `type`) values('6','kimmy','123','2');
insert into `user` (`id`, `account`, `password`, `type`) values('7','tom','123','3');
insert into `user` (`id`, `account`, `password`, `type`) values('8','jimmy','123','4');
insert into `user` (`id`, `account`, `password`, `type`) values('9','sunny','123','1');
insert into `user` (`id`, `account`, `password`, `type`) values('10','limmama','1111','2');
insert into `user` (`id`, `account`, `password`, `type`) values('11','limmama2','11112','3');
insert into `user` (`id`, `account`, `password`, `type`) values('20','limmama3','11113','4');
insert into `user` (`id`, `account`, `password`, `type`) values('21','nanc','1111','1');
insert into `user` (`id`, `account`, `password`, `type`) values('22','nanc2','2221111','2');
insert into `user` (`id`, `account`, `password`, `type`) values('23','nanc3','333','3');
insert into `user` (`id`, `account`, `password`, `type`) values('24',NULL,NULL,'4');
mysql字符串拼接函数介绍
-
CONCAT(string1,string2,…)
说明 : string1,string2代表字符串,concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL
SELECT CONCAT('name=',account) FROM USER;
结果:
-
CONCAT_WS(separator,str1,str2,...)
说明:将多个字符串连接成一个字符串,但是可以一次性指定分隔符~(concat_ws就是concat with separator)
SELECT CONCAT('name=',account) FROM USER;
//分隔符为NULL的情况
SELECT CONCAT_WS(NULL,'name',account,'password',PASSWORD) FROM USER;
-
group_concat()函数
GROUP_CONCAT([DISTINCT] expr [,expr ...] [ORDER BY {unsigned_integer | col_name | expr} [ASC | DESC] [,col_name ...]] [SEPARATOR str_val])
说明:
DISTINCT:去除重复值
expr [,expr ...]:一个或多个字段(或表达式)
ORDER BY {unsigned_integer | col_name | expr} [ASC | DESC] [,col_name ...]:根据字段或表达式进行排序,可多个
SEPARATOR str_val:分隔符(默认为英文逗号)
//这个会按照id倒叙排,然后再拼接
SELECT GROUP_CONCAT(id) FROM USER;
//按照id正序排,然后再拼接
SELECT GROUP_CONCAT(id ORDER BY id ASC) FROM USER;
//指定分隔符
SELECT GROUP_CONCAT(id ORDER BY id ASC SEPARATOR '#') FROM USER;
//多个字段拼接并指定分隔符
SELECT GROUP_CONCAT(id,account ORDER BY id ASC SEPARATOR '--') FROM USER;
//结合GROUP BY查询
SELECT GROUP_CONCAT(id,account ORDER BY id ASC ) FROM USER GROUP BY TYPE;
group_concat()函数 注意事项
group_concat()函数在处理大数据的时候,会发现内容被截取了
其实MYSQL内部对这个是有设置的,默认不设置的长度是1024,如果我们需要更大,就需要手工去修改配置文件
修改方法
在MySQL配置文件中加上
group_concat_max_len = value;
或者修改系统变量:group_concat_max_len
SET [SESSION | GLOBAL] group_concat_max_len = value;