合并具有相同名称列的行并连接场合列

时间:2014-12-09 00:31:37

标签: mysql

我有一个包含许多行的表,这些行具有重复的名称。我想将这些行合并为一个,并连接由逗号(,)分隔的不同的场合列值

因此,在下面的示例中,第2191行将具有场合值:christmas,hannukah,只是因为,并且将删除重复的行。

ID      name                            url                                                     amazon                                                  image                                                   price   relationship                                    occasion                                                    

2191    Bake & Decorate Cupcake Set     http://www.kqzyfj.com/click-7602811-10971062?url=h...   http://amzn.to/1pi7KO2                                  http://www.lduhtrp.net/image-7602811-10971062?imgu...   0.00    sister, daughter, granddaughter, niece, friend  christmas 
4517    Bake & Decorate Cupcake Set     http://www.kqzyfj.com/click-7602811-10971062?url=h...   http://amzn.to/1pi7KO2                                  http://www.lduhtrp.net/image-7602811-10971062?imgu...   0.00    sister, daughter, granddaughter, niece, friend  hannukah 
5902    Bake & Decorate Cupcake Set     http://www.kqzyfj.com/click-7602811-10971062?url=h...   http://amzn.to/1pi7KO2                                  http://www.lduhtrp.net/image-7602811-10971062?imgu...   0.00    sister, daughter, granddaughter, niece, friend  just because    
1660    Ballerina Treasure Music Box    http://www.amazon.com/gp/product/B000FGKDLK/ref=as...   http://www.amazon.com/gp/product/B000FGKDLK/ref=as...   http://ws-na.amazon-adsystem.com/widgets/q?_encodi...   25.99   sister, daughter, granddaughter, niece, friend  birthday    
2798    Ballerina Treasure Music Box    http://www.amazon.com/gp/product/B000FGKDLK/ref=as...   http://www.amazon.com/gp/product/B000FGKDLK/ref=as...   http://ws-na.amazon-adsystem.com/widgets/q?_encodi...   25.99   sister, daughter, granddaughter, niece, friend  christmas   
5155    Ballerina Treasure Music Box    http://www.amazon.com/gp/product/B000FGKDLK/ref=as...   http://www.amazon.com/gp/product/B000FGKDLK/ref=as...   http://ws-na.amazon-adsystem.com/widgets/q?_encodi...   25.99   sister, daughter, granddaughter, niece, friend  hannukah    

1 个答案:

答案 0 :(得分:0)

要确切地确定查询是什么有点困难,但是您想要关闭的路径是聚合和group_concat()

select min(id) as id, name, group_concat(occasion separator ', ')
from table t
group by name;

目前还不清楚输出中您可能想要的其他列。