分组但将所有记录的product_id保存在某处
这是我的查询,
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_description 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;
它返回正确的总和,但它只返回其中一个product_id,
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
我想知道是否有一种方法可以在分组之前收集product_id并将它们添加到a字段中,可能用逗号分隔
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
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_description 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 MySQL 中,您可以使用
GROUP_CONCAT ()
将所有分组值作为单个字段返回。但是,该函数的默认最大长度限制为 1024 个字符,之后它将默默地删除可能显示的任何其他值。您可以使用group_concat_max_len 提高限制
系统变量。当然,在示例查询中,您实际上并未对 p.product_id 字段进行分组,因此这可能不会达到您想要的效果。
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 thegroup_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.