我想从我的查询中获得以下结果:
id_product_attribute | id_product | reference | name | total
12 | 1 | 234235 | product_name | 2
14 | 2 | 235435 | product_name | 7
16 | 3 | 235325 | product_name | 4
等
但是当我使用这个查询时:
select pa.id_product_attribute, p.id_product, pa.reference, cl.name, sum(od.product_quantity) as total
from ps_product_attribute pa
left join ps_order_detail od on od.product_attribute_id = pa.id_product_attribute
left join ps_product p on pa.id_product = p.id_product
left join ps_category_product cp on cp.id_product = p.id_product
left join ps_category_lang cl on cp.id_category = cl.id_category
where cp.id_category = 141 and cl.id_lang = 6;
它只给我这个结果:
id_product_attribute | id_product | reference | name | total
12 | 1 | 234235 | product_name | 13
所以在'total'列中它显示了所有的总数,而不是每行的单独值。
有谁可以告诉我在查询中我做错了什么?
答案 0 :(得分:0)
聚合函数应与GROUP BY
CLAUSE
答案 1 :(得分:0)
您的源数据会有所帮助,但至少您忘记了GROUP BY
。使用当前查询,您应该添加;
GROUP BY pa.id_product_attribute, p.id_product, pa.reference, cl.name
使用MySQL,您可以选择GROUP BY
更少的列并在其他列中随机选择值,但如果可能,您应该GROUP BY
所有没有聚合的列(例如在这种情况下SUM
)。