开发者

group by but save the product_id of all the records somewhere

开发者 https://www.devze.com 2023-03-13 11:03 出处:网络
Here is my query select p.product_id, ts.name as my_name, ss.step_number, p.specs, SUM(p.price),ssp.class_id

Here is my query

select p.product_id, ts.name as my_name, ss.step_number, p.specs, SUM(p.price),  ssp.class_id
from optional_system_step  as ss
join system as s on s.system_id=ss.system_id
join category_descriptio开发者_JS百科n  as cd on cd.category_id=ss.category_id
join optional_system_step_product as  ssp on ss.system_step_id=ssp.system_step_id
join product as p on  p.product_id=ssp.product_id
join product_description as pd on  pd.product_id=p.product_id
join template_step as ts on  (ts.template_id=s.optional_template_id and ts.step_number=ss.step_number)
where s.system_id = '15' 
GROUP BY ssp.class_id, ss.step_number
order by ss.step_number, ssp.class_id; 

which returns the correct sum but it only returns one of the product_id's

78  sdzvzcxv    1       4047.0000   1
143 sdzvzcxv    1       628.0000    2
59  sdzvzcxv    1       962.0000    3
57  another 2       638.0000    1
134 another 2       858.0000    2
62  another 2       548.0000    3
98  xzcvzxc 3       863.0000    1
106 xzcvzxc 3       634.0000    2
98  xzcvzxc 3       1168.0000   3

I was wondering if there is a way to collect the product_id's before the group by and add them in the a field maybe comma seperated

78,55,66    sdzvzcxv    1       4047.0000   1
143,77,88   sdzvzcxv    1       628.0000    2
59,77,88    sdzvzcxv    1       962.0000    3
57,77,88    another 2       638.0000    1
134,77,88   another 2       858.0000    2
62,77,88    another 2       548.0000    3
98,77,890   xzcvzxc 3       863.0000    1
106,76,88   xzcvzxc 3       634.0000    2
98,57,87    xzcvzxc 3       1168.0000   3


In MySQL you can use GROUP_CONCAT() to return all the grouped values as a single field. However, the function has a default max length limit of 1024 characters, after which it'll just silently drop any further values that might have shown up. You can boost the limit with the group_concat_max_len system variable.

Of course, in your sample query, you're not actually grouping on the p.product_id field, so this probably won't have the effect you want.

0

精彩评论

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