帮忙写个查询语句,好人一生平安!

smilesjiang 2011-06-07 05:00:54
ID FLAG
1 Y
1 Y
1 N

2 Y
2 N
2 Y

3 Y
3 Y
3 Y

4 N
4 N
4 N
我要找出FLAG全是Y的,也就是查询结果是3,如何查询?
...全文
150 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jordy127 2011-06-07
  • 打赏
  • 举报
回复
select * from tb t where not exists ( select * from tb where id=t.id and flag='N'
smilesjiang 2011-06-07
  • 打赏
  • 举报
回复
最后一个答案,最周密!!
cd731107 2011-06-07
  • 打赏
  • 举报
回复

select * from tb a
where a.FLAG='Y'
and not exists
(select 1 from tb b where a.id=b.id and b.FLAG<>'Y)'
-晴天 2011-06-07
  • 打赏
  • 举报
回复
select id from tb a where not exists(select 1 from tb where flag='N' and id=a.id)
FlySQL 2011-06-07
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[FLAG] varchar(1))
insert [tb]
select 1,'Y' union all
select 1,'Y' union all
select 1,'N' union all
select 2,'Y' union all
select 2,'N' union all
select 2,'Y' union all
select 3,'Y' union all
select 3,'Y' union all
select 3,'Y' union all
select 4,'N' union all
select 4,'N' union all
select 4,'N'

select * from tb t where not exists(select 1 from tb where id=t.id and flag!='Y')

/**
ID FLAG
----------- ----
3 Y
3 Y
3 Y

(3 行受影响)
**/
FlySQL 2011-06-07
  • 打赏
  • 举报
回复
select * from tb t where not exists(select 1 from tb where id=t.id and flag!='Y')

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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