數(shù)據(jù)準備
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字符串拼接函數(shù)介紹
-
CONCAT(string1,string2,…)
說明 : string1,string2代表字符串,concat函數(shù)在連接字符串的時候,只要其中一個是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()函數(shù)
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 ...]:根據(jù)字段或表達式進行排序良哲,可多個
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()函數(shù) 注意事項
group_concat()函數(shù)在處理大數(shù)據(jù)的時候冕广,會發(fā)現(xiàn)內(nèi)容被截取了
其實MYSQL內(nèi)部對這個是有設置的,默認不設置的長度是1024盈滴,如果我們需要更大涯肩,就需要手工去修改配置文件
修改方法
在MySQL配置文件中加上
group_concat_max_len = value;
或者修改系統(tǒng)變量:group_concat_max_len
SET [SESSION | GLOBAL] group_concat_max_len = value;