按计数大小写分组

i2loujxw  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(383)

我的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

在我的数据库中,我有一个表:
形象

| 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_id | owner_id | product_id | receiver_id |
|---------------------|----------|------------|-------------|
| 1                   | 23       | 27         | 19          |
| 2                   | 38       | 28         | 19          |
| 3                   | 94       | 92         | 90          |

产品

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

我想有一个这样的结果,但我不能组的图像计数。。。

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

我想知道以下产品的交换数量:
0图像
1个图像
2张图片
3张图片
4+个图像
按image.context\uuid分组时的当前输出为:

| Informations | total |
|--------------|-------|
| No image     | 2563  |
| 1 image      | 1     |
| 1 image      | 3     |
| 1 image      | 1     |
| 1 image      | 2     |
djmepvbi

djmepvbi1#

使用 Left Joinimage 表中,以便也考虑那些没有图像(没有匹配行)的情况。
你需要先计算每一张照片的图片数量 product_id 明确地。
现在,将这些计数结果用作派生表,然后 Count 产品id取决于其所在的类别(“无图像”到“4和+图像”)
注意,在mysql中 Select 子句可以原样使用,在 Group By , Having 以及 Order By 条款。
请尝试以下操作:

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`
3df52oht

3df52oht2#

我会使用一个内联视图(派生表)来获取每个产品id的图像计数(在paren中运行查询以查看返回的结果)。这应该是每一个不同的产品id,以及与产品相关的令人毛骨悚然的图像计数。
将内联视图的结果与 exchange_product table。在外部查询中,我们可以按funky计数进行分组。

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

相关问题