gpt4 book ai didi

php - 通过添加值进行分组

转载 作者:行者123 更新时间:2023-11-29 04:56:16 26 4
gpt4 key购买 nike

我有这个问题

select ts.name as my_name, ss.step_number, p.specs, 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' order by ss.step_number, ssp.class_id;

返回这个

admin   1       999.0000    1   
admin 1 1349.0000 1
admin 1 1699.0000 1
pay 1 479.0000 2
pay 1 149.0000 2
pay 1 269.0000 3

看起来不错,但问题是我需要按 class_id 分组,但在价格字段中我需要添加三个价格,因此例如我将返回这两行

admin   1       4047.0000   1   
pay 1 897.0000 2

所以基本上我想将三个数字加在一起并在价格字段中返回该值

最佳答案

将聚合函数 SUM()GROUP BY 结合使用:

select 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 ts.name, ss.step_number, p.spects, ssp.class_id
order by ss.step_number, ssp.class_id;

关于php - 通过添加值进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6363028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com