Using group by on two fields and count in SQL

后端 未结 3 1328
长情又很酷
长情又很酷 2020-11-29 21:43

I have a table in my mysql db that has two columns: group and subgroup. See below.

 group, subGroup
 grp-A, sub-A
 grp-A, sub-A
 grp-A, sub-B      
 grp-B         


        
3条回答
  •  孤独总比滥情好
    2020-11-29 21:54

    You must group both columns, group and sub-group, then use the aggregate function COUNT().

    SELECT
      group, subgroup, COUNT(*)
    FROM
      groups
    GROUP BY
      group, subgroup
    

提交回复
热议问题