MySQL计数分组后特定列出现的次数

时间:2018-05-16 13:54:01

标签: mysql sql

这是我的疑问:

SELECT 
    mh.super_category AS parentDescription, 
    mh.super_category AS parentSlug, 
    mh.description AS subDescription, 
    mh.slug AS subSlug, 
    COUNT(mh.slug) AS qty,
    cg.Description AS nestedSubDescription, 
    cg.slug AS nestedSubSlug 
FROM Categories AS c
    INNER JOIN Category_Groups AS cg ON (c.Category_Group=cg.CategoryCode) 
    INNER JOIN menu_heading AS mh ON (cg.MenuHeading = mh.id)
    INNER JOIN tbl_store_brands ON (tbl_store_brands.categoryID = c.pkID) 
WHERE c.seq <> 127 
GROUP BY cg.slug 
ORDER BY mh.super_category, mh.seq ASC, cg.seq, c.seq, c.Category_Group, c.Category_ID

这是返回结果的表示:

+-------------------+------------+----------------+--------------------+-----+--------------------------+--------------------------+
| parentDescription | parentSlug | subDescription |      subSlug       | qty |   nestedSubDescription   |      nestedSubSlug       |
+-------------------+------------+----------------+--------------------+-----+--------------------------+--------------------------+
| Appliances        | Appliances | Washers/Dryers | washers-dryers     |  50 | Washers                  | washers                  |
| Appliances        | Appliances | Washers/Dryers | washers-dryers     |  68 | Dryers                   | dryers                   |
| Appliances        | Appliances | Washers/Dryers | washers-dryers     |   5 | Stack Laundry            | stack-laundry            |
| Appliances        | Appliances | Washers/Dryers | washers-dryers     |  13 | Laundry Accessories      | laundry-accessories      |
| Appliances        | Appliances | Refrigerators  | refrigerators      |  48 | French Door              | french-door              |
| Appliances        | Appliances | Refrigerators  | refrigerators      |  40 | Side by Side             | side-by-side             |
| Appliances        | Appliances | Refrigerators  | refrigerators      |  24 | Bottom Freezer           | bottom-freezer           |
| Appliances        | Appliances | Refrigerators  | refrigerators      |  16 | Top Mount                | top-mount                |
| Appliances        | Appliances | Refrigerators  | refrigerators      |  37 | Built In                 | built-in                 |
| Appliances        | Appliances | Refrigerators  | refrigerators      |  82 | Specialty                | specialty                |
| Appliances        | Appliances | Refrigerators  | refrigerators      |  66 | Freezers                 | freezers                 |
| Appliances        | Appliances | Refrigerators  | refrigerators      |  20 | Refrigerator Accessories | refrigerator-accessories |
+-------------------+------------+----------------+--------------------+-----+--------------------------+--------------------------+

qty列中返回的数字是mh.slug在没有组条件的情况下出现的次数,但我实际想要的是它在查询中显示的次数,即4次用于洗衣干衣机。

我怎样才能做到这一点?

新样本结果

+--------------------------+--------------------------+---+------------------------------------+-------------------------------------------+
| Refrigerators | refrigerators | French Door              | french-door              | 9 | Cabinet Depth: French Door         | cabinet-depth-french-door-refrigerators   |
| Refrigerators | refrigerators | Side by Side             | side-by-side             | 9 | Side x Side: with Dispenser        | side-by-side-with-dispenser-refrigerators |
| Refrigerators | refrigerators | Bottom Freezer           | bottom-freezer           | 9 | Bottom Freezer                     | bottom-freezer-refrigerators              |
| Refrigerators | refrigerators | Top Mount                | top-mount                | 9 | Top Freezer                        | top-freezer-refrigerators                 |
| Refrigerators | refrigerators | Built In                 | built-in                 | 9 | Built-in Columns Refrigerator Only | built-in-columns-refrigerator-only        |
| Refrigerators | refrigerators | Specialty                | specialty                | 9 | Wine Storage                       | wine-storage                              |
| Refrigerators | refrigerators | Freezers                 | freezers                 | 9 | Icemaker Kits                      | icemaker-kits-refrigerator                |
| Refrigerators | refrigerators | Refrigerator Accessories | refrigerator-accessories | 9 | Refrigerator Accessories           | refrigeration-accessories                 |
+---------------+---------------+--------------------------+--------------------------+---+------------------------------------+-------------------------------------------+

1 个答案:

答案 0 :(得分:1)

SELECT 
    mh.super_category AS parentDescription, 
    mh.super_category AS parentSlug, 
    mh.description AS subDescription, 
    mh.slug AS subSlug, 
    (SELECT COUNT(*) FROM Category_Groups AS countCg WHERE countCg.MenuHeading = mh.id) As qty,
    cg.Description AS nestedSubDescription, 
    cg.slug AS nestedSubSlug 
FROM Categories AS c
    INNER JOIN Category_Groups AS cg ON (c.Category_Group=cg.CategoryCode) 
    INNER JOIN menu_heading AS mh ON (cg.MenuHeading = mh.id)
    INNER JOIN tbl_store_brands ON (tbl_store_brands.categoryID = c.pkID) 
WHERE c.seq <> 127 
GROUP BY cg.slug 
ORDER BY mh.super_category, mh.seq ASC, cg.seq, c.seq, c.Category_Group, c.Category_ID

对于你的第二个问题,试试这个,让我知道它是否有效(没有时间认真100%通过,我不知道关系类型,所以这只是试验和错误):

SELECT 
    mh.super_category AS parentDescription, 
    mh.super_category AS parentSlug, 
    mh.description AS subDescription, 
    mh.slug AS subSlug, 
    cg.Description AS nestedSubDescription, 
    cg.slug AS nestedSubSlug 
    (SELECT COUNT(countCg.*) FROM Categories AS c2 
          INNER JOIN Category_Groups AS countCg 
          INNER JOIN tbl_store_brands tsb2 ON (tsb2 .categoryID = c2.pkID)
      WHERE countCg.MenuHeading = mh.id) As qty
FROM Categories AS c
    INNER JOIN Category_Groups AS cg ON (c.Category_Group=cg.CategoryCode) 
    INNER JOIN menu_heading AS mh ON (cg.MenuHeading = mh.id)
    INNER JOIN tbl_store_brands ON (tbl_store_brands.categoryID = c.pkID) 
WHERE c.seq <> 127 
GROUP BY cg.slug 
ORDER BY mh.super_category, mh.seq ASC, cg.seq, c.seq, c.Category_Group, c.Category_ID