简体   繁体   English

MySQL-按计数分组

[英]MySQL - Group by case of count

My MySQL query is : 我的MySQL查询是:

SELECT 
CASE 
    WHEN count(`image`.`context_uuid`) = 0 THEN 'No image' 
    WHEN count(`image`.`context_uuid`) = 1 THEN '1 image' 
    WHEN count(`image`.`context_uuid`) = 2 THEN '2 images' 
    WHEN count(`image`.`context_uuid`) = 3 THEN '3 images' ELSE '4 and + images' 
END AS `Informations`, count(`exchange_product`.`exchange_product_id`) AS `total`
FROM `exchange_product`
INNER JOIN `product` ON `product`.`product_id` = `exchange_product`.`product_id`
INNER JOIN `image` ON `image`.`context_uuid` = `product`.`product_uuid`
GROUP BY 
CASE 
    WHEN count(`image`.`context_uuid`) = 0 THEN 'No image' 
    WHEN count(`image`.`context_uuid`) = 1 THEN '1 image' 
    WHEN count(`image`.`context_uuid`) = 2 THEN '2 images' 
    WHEN count(`image`.`context_uuid`) = 3 THEN '3 images' ELSE '4 and + images' 
END

In my DB, I have a table : 在我的数据库中,我有一个表:

image 图片

| image ID     | url         | context_type | context_uuid  |
|--------------|-------------|--------------|---------------|
| 1            | www.az.com  | user         | 19            |
| 2            | www.az.com  | product      | 27            |
| 3            | www.az.com  | product      | 27            |
| 4            | www.az.com  | product      | 28            |

exchange_product exchange_product

| exchange_product_id | owner_id | product_id | receiver_id |
|---------------------|----------|------------|-------------|
| 1                   | 23       | 27         | 19          |
| 2                   | 38       | 28         | 19          |
| 3                   | 94       | 92         | 90          |

product 产品

| product_id   | user_id     | name       | product_uuid |
|--------------|-------------|------------|--------------|
| 1            | 23          | something  | 28           |
| 2            | 38          | something  | 12           |
| 3            | 94          | something  | 23           |

I want to have a result like this but I can't group by count of image... 我想要这样的结果,但无法按图像计数分组...

| Informations | total |
|--------------|-------|
| No image     | 2563  |
| 1 image      | 1029  |
| 2 images     | 567   |
| 3 images     | 180   |
| 4 + images   | 1928  |

I want to know the count of exchanges of product that have : 我想知道以下产品交换的数量:

  • 0 image 0张图片
  • 1 image 1张图片
  • 2 images 2张图片
  • 3 images 3张图片
  • 4+ images 4张以上图片

My current output when I GROUP BY image.context_uuid is : 当我GROUP BY image.context_uuid时,我当前的输出是:

| Informations | total |
|--------------|-------|
| No image     | 2563  |
| 1 image      | 1     |
| 1 image      | 3     |
| 1 image      | 1     |
| 1 image      | 2     |
  • Use Left Join on image table, in order to consider those cases also, where there is no image (no matching row). image表上使用“ Left Join ”,以便还考虑没有图像(没有匹配行)的情况。
  • You need to first count the number of images for every product_id specifically. 您需要首先统计每个product_id的图片数量。
  • Now, use these count results as a Derived Table , and Count the product_id(s) depending on which category they lie in ('No image' to '4 and + images') 现在,将这些计数结果用作“ 派生表” ,并根据product_id所处的类别对其Count (“无图像”到“ 4和+图像”)
  • Note that in MySQL , the Alias defined in the Select clause can be used as it is, in the Group By , Having and Order By clauses. 请注意, 在MySQL中 ,可以在Group ByHavingOrder By子句中Group By原样使用Select子句中定义的别名。

Try the following: 请尝试以下操作:

SELECT 
  CASE 
    WHEN dt.images_count = 0 THEN 'No image' 
    WHEN dt.images_count = 1 THEN '1 image' 
    WHEN dt.images_count = 2 THEN '2 images' 
    WHEN dt.images_count = 3 THEN '3 images' 
    ELSE '4 and + images' 
  END AS `Informations`, 

  COUNT(dt.product_id) AS `total` 
FROM 
(
  SELECT 
    `exchange_product`.`product_id`, 
    COUNT(`image`.`context_uuid`) AS images_count 
  FROM `exchange_product`
  LEFT JOIN `product` ON `product`.`product_id` = `exchange_product`.`product_id`
  LEFT JOIN `image` ON `image`.`context_uuid` = `product`.`product_uuid`
  GROUP BY 
    `exchange_product`.`product_id`
) AS dt 
GROUP BY `Informations`

I'd use an inline view (derived table) to get a count of images for each product_id. 我将使用内联视图(派生表)来获取每个product_id的图片计数。 (Run the query inside the parens to see the result that is returned. That should be each distinct product_id, with the funky count of images related to the product. (在括号内运行查询,以查看返回的结果。该结果应该是每个不同的product_id,以及与该产品相关的图像的时髦计数。

Join the result of the inline view to the exchange_product table. 将内联视图的结果连接到exchange_product表。 And the outer query, we can do the GROUP BY on the funky count. 和外部查询,我们可以对时髦的计数进行GROUP BY。

SELECT q.image_cnt                          AS `Informations`
     , COUNT(e.product_id)                  AS `total`
  FROM ( -- count of images by product_id
         SELECT p.product_id
              , CASE COUNT(i.context_uuid)
                WHEN 0 THEN 'No image'
                WHEN 1 THEN '1 image'
                WHEN 2 THEN '2 images'
                WHEN 3 THEN '3 images'
                ELSE '4 and + images'
                END AS image_cnt
           FROM `product` p
           LEFT
           JOIN `image` i
             ON i.context_uuid = p.product_uuid
          GROUP
             BY p.product_id
       ) q
  JOIN `exchange_product` e
    ON e.product_id = q.product_id
 GROUP
    BY q.image_cnt
 ORDER
    BY q.image_cnt+0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM