mysql - 选择不同的,在行尾显示计数

标签 mysql select count distinct

我有这个表/数据结构:

ID    Area       Postcode
------------------------
1     "Area 1"   "EN1 1NE"
2     "Area 2"   "AB2 3BA"
3     "Area 1"   "EN1 1NE"
4     "Area 3"   "XY4 5ZA"
5     "Area 4"   "MN6 5OP"

第 1 行和第 3 行具有重复的邮政编码和区域。我需要一个像这样输出计数的查询:

Area       Postcode    Count
----------------------------
"Area 1"   "EN1 1NE"   2
"Area 2"   "AB2 3BA"   1
"Area 3"   "XY4 5ZA"   1
"Area 4"   "MN6 5OP"   1

我已经搜索并尝试使用 DISTINCT 和/或 COUNT 和/或子查询,但在这里真的迷路了! #weak-sql-fu

谢谢! 本

最佳答案

您可以像这样将 count(*) 与 group by 一起使用:

select Area, Postcode, count(*) from TABLE group by Area, Postcode;

关于mysql - 选择不同的,在行尾显示计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9192777/

相关文章:

mysql innodb缓冲区与内存表性能

php - 语法错误或访问冲突 1064 php pdo

javascript - Angular ng-repeat 内嵌套 ng-option

mysql - 使用其他表中的 WHERE 计算最多出现次数

mysql - 连接多个数据库mysql

mysql - docker-compose.yml spring boot mysql配置

mysql - 如何将mysql数据渲染到jade文件?

MYSQL - 如何选择 CASE(两个表,如果一个不存在,请检查另一个)

java - 如何获取Jtree Java中所有父节点的叶节点数

php - 计算两个日期之间超过 1 小时的天数再加 1 天