求一sql

echoxue 2012-07-13 05:20:17
A表
ID status
0001 bad
0002 good
0001 good
0002 bad
0003 good
0004 good
0005 good
0001 bad
0003 bad



ID status
004 good
005 good

就是只要一个ID出现的status是有2次以上的状态:bad和good,这个数据就不要了
...全文
91 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
echoxue 2012-07-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

select id,status
from tb
group by id
having count(distinct status)<=1
[/Quote]

这个还算快,但是这个count(distinct status)效率是不是低啊
,这个有点不懂了,为什么不把count(distinct status)<=放在select语句中啊,能否解释以下啊?
rucypli 2012-07-13
  • 打赏
  • 举报
回复
select id,status
from tb
group by id
having count(distinct status)<=1
echoxue 2012-07-13
  • 打赏
  • 举报
回复
2位不行啊,数据多,执行效率n低

我在考虑分组统计是否快
WWWWA 2012-07-13
  • 打赏
  • 举报
回复



SELECT * from tta a where not exists(select 1 from tta where a.ID=id and a.status<>status)
ACMAIN_CHM 2012-07-13
  • 打赏
  • 举报
回复
select * from A t where not Exists ( select 1 from A where ID=t.ID And status!= t.status)

56,687

社区成员

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

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