知道我该怎么做吗?

我想按故障,流和零件进行分组,但在这些分组中,我要按频率排序?

请帮忙。

米克

select
p.part_no as Part,
p.product_description as Description,
p.brand_id as Brand,
p.part_product_family as Stream,
p.range as Range_Name,
fa.description as Area,
f.description as Fault,
count(f.description) as Frequency

from recorded_faults rf, faults f, fault_areas fa, losses l, products p


WHERE rf.fault_id = f.id
and f.fault_area_id = fa.id
and rf.loss_id = l.id
and p.id = l.product_id

group by
Fault,
Stream,
Part

order by Frequency desc

最佳答案

您可以尝试以下方法:

select
p.part_no as Part,
p.product_description as Description,
p.brand_id as Brand,
p.part_product_family as Stream,
p.range as Range_Name,
fa.description as Area,
f.description as Fault,
count(f.description) as Frequency
from recorded_faults rf inner join faults f on rf.fault_id = f.id
                        inner join fault_areas fa on f.fault_area_id = fa.id
                        inner join losses l on rf.loss_id = l.id
                        inner join products p on p.id = l.product_id

group by
p.part_no,
p.product_description,
p.brand_id,
p.part_product_family,
p.range,
fa.description,
f.description,
order by count(f.description) desc

尽量避免逗号分隔的JOINS,不再建议使用
另外,您需要将select的列放在GROUP BY

关于mysql - MySQL的顺序和分组依据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37161501/

10-16 14:50