开发者

SQL Query Question ROW_CONCAT

开发者 https://www.devze.com 2022-12-31 17:09 出处:网络
I have been stuck on this problem for quite awhile... I hope so开发者_如何学JAVAmeone out there can give me a hand.

I have been stuck on this problem for quite awhile... I hope so开发者_如何学JAVAmeone out there can give me a hand.

The following table is in my database:

Product_ID Color   Type
1          Red     Leather
1          Silver  Metal
1          Blue    Leather
2          Orange  Metal
2          Purple  Metal

I am trying to get the following output:

Product_ID Type    Color
1          Leather Red, Blue
1          Metal   Silver
2          Metal   Orange, Purple

I know it has to do with some kind of double group by and a group_concat.... have been looking at this for an hour without figuring it out. Any help is much appreciated!!!


Try this

SELECT Product_ID, Type, GROUP_CONCAT(Color)
FROM Products
GROUP BY Product_ID, Type

You didn't mention which database you are using - but I'm assuming MySQL since you mention GROUP_CONCAT.

0

精彩评论

暂无评论...
验证码 换一张
取 消