有这样一个表,数据是这样的
| id | name |
+------+------+
| 1 | a |
| 1 | b |
| 1 | c |
| 2 | d |
| 2 | e |
通过分组查询可以得到这样的结果
select count(id) from a group by id;
+-----------+
| count(id) |
+-----------+
| 3 |
| 2 |
+-----------+现在的要求是我想分组统计相同id的值出现过次数>2次的记录。
比如上面:
| 2 | d |
| 2 | e |这样记录应该舍去,如果是你,你会怎么写SQL语句?
| id | name |
+------+------+
| 1 | a |
| 1 | b |
| 1 | c |
| 2 | d |
| 2 | e |
通过分组查询可以得到这样的结果
select count(id) from a group by id;
+-----------+
| count(id) |
+-----------+
| 3 |
| 2 |
+-----------+现在的要求是我想分组统计相同id的值出现过次数>2次的记录。
比如上面:
| 2 | d |
| 2 | e |这样记录应该舍去,如果是你,你会怎么写SQL语句?
解决方案 »
- 插入数据疑惑
- MySql语句效率问题
- mysql三天两头出错打不开,帮忙看看是什么问题,已经添加my.ini配置文件
- mysql与sql server的链接问题
- Communications link failure due to underlying exception:
- [求解]Postgresql下select語法實現
- mysql c api 一问
- 数据库两张表关联查询问题
- 查询一行中为NULL或为空的字段数量
- Python中PyMySQL提示AttributeError: module 'socket' has no attribute 'AF_UNIX'
- Linux下C程序连接MySQL数据出错
- mysql数据库表从服务器到客户端的备份
where 2<(select count(*) from 有这样一个表 where id=a.id)
from tb
where id in (select id from tb group by id having count(*)>2)
(select id from tt group by id having count(*)>2) b
on a.id=b.id
(select id from tt group by id having count(*)>2) b
on a.id=b.id楼上的回复很好了。