这样的sql语句你会怎么写?

qxh0724 2011-05-24 07:36:05
有这样一个表,数据是这样的
| 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语句?
...全文
88 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jimzhke 2011-06-06
  • 打赏
  • 举报
回复
select a.* from tt a inner join
(select id from tt group by id having count(*)>2) b
on a.id=b.id

楼上的回复很好了。。。。
WWWWA 2011-05-25
  • 打赏
  • 举报
回复
select a.* from tt a inner join
(select id from tt group by id having count(*)>2) b
on a.id=b.id
sjzzy 2011-05-25
  • 打赏
  • 举报
回复
学习了
rucypli 2011-05-24
  • 打赏
  • 举报
回复
select *
from tb
where id in (select id from tb group by id having count(*)>2)
ACMAIN_CHM 2011-05-24
  • 打赏
  • 举报
回复
select * from 有这样一个表 a
where 2<(select count(*) from 有这样一个表 where id=a.id)

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧