mysql中如何用GROUP_CONCAT group by将字符串字段拼接起来

时间:2022-04-27 19:09:46
CREATE TABLE `jxc_orderdetail` (
`detail_id` int(11) NOT NULL AUTO_INCREMENT,
`order_no` varchar(20) DEFAULT '' COMMENT '订单编号',
`pro_no` varchar(20) DEFAULT '' COMMENT '产品编号',
`pro_name` varchar(100) DEFAULT '' COMMENT '产品名字',
`pro_number` int(11) DEFAULT '0' COMMENT '产品数量',
`pro_price` decimal(10,2) DEFAULT '0.00' COMMENT '产品单价',
`pay_price` decimal(10,2) DEFAULT '0.00' COMMENT '最后支付',
PRIMARY KEY (`detail_id`)
) ENGINE=MyISAM AUTO_INCREMENT=139707 DEFAULT CHARSET=utf8;

/*Data for the table `jxc_orderdetail` */

insert into `jxc_orderdetail`(`detail_id`,`order_no`,`pro_no`,`pro_name`,`pro_number`,`pro_price`,`pay_price`) values (139704,'H140523000001','130001','美国莎娜琳',2,'78.00','1000.00'),(139705,'H140524000001','49101815101237','移花宫速效细肤祛痘乳100ML',1,'133.00','1000.00'),(139706,'H140524000001','ECS000160','移花宫速效控油嫩肤水12ML',1,'39.00','1000.00');


执行如下语句:


SELECT order_no,GROUP_CONCAT(pro_name,'*',pro_number) FROM jxc_orderdetail GROUP BY order_no
结果:


mysql中如何用GROUP_CONCAT   group by将字符串字段拼接起来

本例子是多个字段聚合,如果只是一个字段 GROUP_CONCAT 里面只有一个参数即可