Using group by on two fields and count in SQL(在两个字段上使用 group by 并在 SQL 中计数)
问题描述
我的 mysql 数据库中有一个表,它有两列:组和子组.见下文.
组,子组grp-A, sub-Agrp-A, sub-Agrp-A, sub-Bgrp-B, sub-Agrp-B, sub-Bgrp-B, sub-B我正在尝试获取每个唯一情侣组/子组的记录数.
这是我所期望的:
组、子组、计数grp-A, sub-A, 2grp-A, sub-B, 1grp-B, sub-A, 1grp-B, sub-B, 2在阅读了一些帖子后,我尝试了使用 group by、count() 的几个 sql 查询,但我没有得到预期的结果.我该如何解决这个问题?
我想你在找:SELECT a, b, COUNT(a) FROM tbl GROUP BY a, b>
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.
This is what I expect:
group, subGroup, count
grp-A, sub-A, 2
grp-A, sub-B, 1
grp-B, sub-A, 1
grp-B, sub-B, 2
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?
I think you're looking for: SELECT a, b, COUNT(a) FROM tbl GROUP BY a, b
这篇关于在两个字段上使用 group by 并在 SQL 中计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在两个字段上使用 group by 并在 SQL 中计数
基础教程推荐
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 无法解决整理冲突 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 是否可以执行按位分组功能? 2021-01-01
