mysql中使用sql根据相同字段合并值
程序员文章站
2022-05-09 21:09:14
...
1、GROUP_CONCAT函数的用法
这是原始表
这是通过group_concat函数从查询出来的数据
sql语句:
select a.sales_order_no,
REPLACE(GROUP_CONCAT(a.external_order_no),',',' / ') as external_order_no,
REPLACE(GROUP_CONCAT(a.original_order_no),',',' / ') as original_order_no
from order_temp_001 as a
GROUP BY a.sales_order_no;
这里的REPLACE语法是代替原来group_concat语法的逗号分隔符替换成了/符号
默认用法:
GROUP_CONCAT(a.external_order_no),就会用逗号分隔开
提一下:在sqlserver中拼接的写法使用stuff语法
实例:
select FoodRoom,
stuff((select ‘,’+name from MO_Cook
where c.FoodRoom=FoodRoom
for xml path(’’)),1,1,’’) as CookNames
from MO_Cook c
group by c.FoodRoom