mysql – 在单个查询中进行分组,排序和计数

我正在尝试GROUP,SORT和COUNT在我的一个名为’商品’的表中的一个查询中.

这是我的MySql表的简化:

family  sub_family  name        detailed_name 
Agro    Grains      Wheat       Wheat per 1 mt
Agro    Grains      Corn        Corn per 1 mt
Agro    Grains      Sugar       Sugar per 1 mt
Agro    Fruits      Apple       Apple red
Agro    Fruits      Apple       Apple green
Agro    Fruits      Apple       Apple yellow
Agro    Fruits      Lemon       Lemon classic
Wood    Tree        Lemon       Lemon in logs
Wood    Tree        Oak         Oak in logs
Wood    Tree        Epicea      Epicea in logs
Wood    Packaging   Kraftliner  Krafliner 3mm

我想要 :

>按名称分组
>按家庭,次家庭和最后名称排序
> COUNT每个系列的行数,sub_family然后命名(在同一个sub_family)

到目前为止,我设法在同一个sub_family中执行除COUNT之外的所有操作.

的确,以下查询:

SELECT 
    TableC.family,
    TableC.NbrFamily,
    TableB.sub_family,
    TableB.NbrSubFamily,
    TableA.name,
    TableA.NbrName
FROM 
(
    SELECT 
        family,
        sub_family,
        name,
        COUNT(DISTINCT commodities.id) AS NbrName 
    FROM commodities GROUP BY name
) TableA
INNER JOIN 
(
    SELECT 
        sub_family,
        COUNT(DISTINCT commodities.id) AS NbrSubFamily 
    FROM commodities GROUP BY sub_family
) TableB
ON (TableA.sub_family = TableB.sub_family)
INNER JOIN 
(
    SELECT 
        family,
        COUNT(DISTINCT commodities.id) AS NbrFamily 
    FROM commodities GROUP BY family
) TableC
ON (TableA.family = TableC.family)
GROUP BY TableA.name
ORDER BY TableA.family,TableA.sub_family,TableA.name

这导致以下结果:

family  NbrFamily  sub_family  NbrSubFamily  name        NbrName
Agro    7          Grains      3             Wheat       1       
Agro    7          Grains      3             Corn        1        
Agro    7          Grains      3             Sugar       1       
Agro    7          Fruits      4             Apple       3
Agro    7          Fruits      4             Lemon       2
Wood    4          Tree        3             Lemon       2
Wood    4          Tree        3             Oak         1     
Wood    4          Tree        3             Epicea      1  
Wood    4          Packaging   1             Kraftliner  1

您可以看到NbrName计算柠檬2次,但我希望它只计算一次因为一个柠檬在Fruits sub_family而另一个柠檬在Tree sub_family.

[更新]:这是我想要的结果:

   family  NbrFamily  sub_family  NbrSubFamily  name        NbrName
    Agro    7          Grains      3             Wheat       1       
    Agro    7          Grains      3             Corn        1        
    Agro    7          Grains      3             Sugar       1       
    Agro    7          Fruits      4             Apple       3
    Agro    7          Fruits      4             Lemon       1
    Wood    4          Tree        3             Lemon       1
    Wood    4          Tree        3             Oak         1     
    Wood    4          Tree        3             Epicea      1  
    Wood    4          Packaging   1             Kraftliner  1

最佳答案 只是我猜你要求
http://sqlfiddle.com/#!9/e9206/16

因为它带来了预期的结果:

 SELECT A.family, C.NbrFamily,A.sub_family,B.NbrSubFamily,A.name,COUNT(A.Name)
 FROM  commodities as A
LEFT JOIN (
  SELECT family,sub_family,COUNT(Name) AS NbrSubFamily 
  FROM commodities 
  GROUP BY family,sub_family
) B
ON A.sub_family = B.sub_family 
  AND A.family = B.family 
LEFT JOIN (
  SELECT family,COUNT(Name) AS NbrFamily 
  FROM commodities 
  GROUP BY family
) C

ON  A.family = C.family 
GROUP BY A.family,A.sub_family,A.name
ORDER BY A.id
点赞