mysql分组并多⾏拼接--group_concat和groupby的使⽤
– 创建表结构
DROP TABLE IF EXISTS exe;
CREATE TABLE exe (
id int(3) NOT NULL,
type int(3) default NULL,
name varchar(10) default NULL,
groupby分组other int(3) default NULL,
text int(255) default NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
– 插⼊测试数据
INSERT INTO exe VALUES (‘1’, ‘1’, ‘分拼’, ‘2’, ‘1’);
INSERT INTO exe VALUES (‘2’, ‘1’, ‘四维’, ‘3’, ‘2’);
INSERT INTO exe VALUES (‘3’, ‘2’, ‘总评’, ‘1’, ‘4’);
INSERT INTO exe VALUES (‘4’, ‘3’, ‘季度’, ‘5’, ‘3’);
– group_concat和group by的使⽤
– 默认逗号连接
pe,group_concat(t.name) “result” from exe t group pe;
– separator指定连接符
pe,group_concat(t.name separator ‘;’) “result” from exe t group pe;
– 排序连接
pe,group_concat(t.name order her desc) “result” from exe t group pe;
pe,group_concat(t.name order her) “result” from exe t group pe;
– 低版本mysql连接数字会返回BLOB⼤对象,需要⽤cast()转换成char类型
pe,group_concat(t.name) “name”,group_her) “result” from exe t group pe;
pe,group_concat(t.name) “name”,group_concat(her AS char)) “result” from exe t group pe;参考⽂章:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论