在两个字段上使用分组依据并在SQL中计数 [英] Using group by on two fields and count in SQL

查看:330
本文介绍了在两个字段上使用分组依据并在SQL中计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的mysql数据库中有一个表,该表有两列:group和subgroup.见下文.

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, sub-A
 grp-B, sub-B
 grp-B, sub-B

我正在尝试获取每个唯一的夫妇组/子组的记录数.

I am trying to get the number of records for each unique couple group/subGroup.

这是我的期望:

group, subGroup, count
grp-A, sub-A, 2
grp-A, sub-B, 1
grp-B, sub-A, 1
grp-B, sub-B, 2

在阅读了一些帖子之后,我尝试使用group by count()进行了一些SQL查询,但是我无法获得预期的结果.我该如何解决?

After reading some posts I tried several sql queries using group by, count(), but I do not manage to get the expected result. How can I fix this?

推荐答案

我认为您正在寻找:SELECT a, b, COUNT(a) FROM tbl GROUP BY a, b

这篇关于在两个字段上使用分组依据并在SQL中计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆