簡體   English   中英

MySQL-按計數分組

[英]MySQL - Group by case of count

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

| 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張以上圖片

當我GROUP BY image.context_uuid時,我當前的輸出是:

| Informations | total |
|--------------|-------|
| No image     | 2563  |
| 1 image      | 1     |
| 1 image      | 3     |
| 1 image      | 1     |
| 1 image      | 2     |
  • image表上使用“ Left Join ”,以便還考慮沒有圖像(沒有匹配行)的情況。
  • 您需要首先統計每個product_id的圖片數量。
  • 現在,將這些計數結果用作“ 派生表” ,並根據product_id所處的類別對其Count (“無圖像”到“ 4和+圖像”)
  • 請注意, 在MySQL中 ,可以在Group ByHavingOrder By子句中Group By原樣使用Select子句中定義的別名。

請嘗試以下操作:

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`

我將使用內聯視圖(派生表)來獲取每個product_id的圖片計數。 (在括號內運行查詢,以查看返回的結果。該結果應該是每個不同的product_id,以及與該產品相關的圖像的時髦計數。

將內聯視圖的結果連接到exchange_product表。 和外部查詢,我們可以對時髦的計數進行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