简体   繁体   中英

mysql - how do I get count of counts

I have a table with duplicate skus.

skua
skua

skub
skub
skub

skuc
skuc

skud

    SELECT sku, COUNT(1) AS `Count` FROM products GROUP BY sku;

shows me all the skus that have duplicates and the number of duplicates

    skua 2
    skub 3
    skuc 2
    skud 1

I am trying to find how many there are with 2 duplicates, 3 duplicates etc.

ie

  duplicated  count
       1        1      (skud)
       2        2      (skua, and skuc)
       3        1      (skub) 

and I don't know how to write the sql. I imagine it needs a subselect...

thanks

select dup_count as duplicated,
       count(*) as `count`, 
       group_concat(sku) as skus
from
(
    SELECT sku, COUNT(1) AS dup_count
    FROM products 
    GROUP BY sku
) tmp_tbl
group by dup_count

Just use your current query as an inline view, and use the rows from that just like it was from a table.

eg

SELECT t.Count  AS `duplicated`
     , COUNT(1) AS `count`
  FROM ( SELECT sku, COUNT(1) AS `Count` FROM products GROUP BY sku ) t
 GROUP BY t.Count

MySQL refers to an inline view as a "derived table", and that name makes sense, when we understand how MySQL actually processes that. MySQL runs that inner query, and creates a temporary MyISAM table; once that is done, MySQL runs the outer query, using the temporary MyISAM table. (You'll see that if you run an EXPLAIN on the query.)

Above, I left your query just as you formatted it; I'd tend to reformat your query, so that entire query looks like this:

SELECT t.Count  AS `duplicated'
     , COUNT(1) AS `count`
  FROM ( SELECT p.sku
              , COUNT(1) AS `Count` 
           FROM products p 
          GROUP BY p.sku 
       ) t
 GROUP BY t.Count

(Just makes it easier for me to see the inner query, and easier to extract it and run it separately. And qualifying all column references (with a table alias or table name) is a best practice.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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