如何使用两个连接对表中的group_concat进行分组

时间:2015-10-06 16:26:03

标签: mysql

目前我有一个产品表,一个用于价格,一个用于浇头,我使用以下代码构建我的JSON文件

SELECT `products`.`PrID` as `id`, `name`, group_concat(product_prices.size)as sizes, group_concat(product_prices.price)as prices, group_concat(product_toppings.IID)as IIDs
FROM `products`
JOIN `product_prices` ON `product_prices`.`PrID` = `products`.`PrID`
JOIN `product_toppings` ON `product_toppings`.`PrID` = `products`.`PrID`
WHERE `products`.`PrID` = '2'
AND `products`.`status` = 'active'
GROUP BY `products`.`PrID` 

我目前的结果:

{"id":"2","name":"Hawaiian","sizes":"medium,medium,medium,medium,large,large,large,large,supersize,supersize,supersize,supersize","prices":"11.99,11.99,11.99,11.99,14.99,14.99,14.99,14.99,16.99,16.99,16.99,16.99","IIDs":"7,18,22,28,7,18,22,28,7,18,22,28"}

我知道我的问题在于分组,但我无法弄清楚如何将其分组,以便我得到类似的内容:

{"id":"2","name":"name","sizes":"medium,large,large,supersize","prices":"11.99,14.99,16.99","IIDs":"7,18,22,28"}

0 个答案:

没有答案