求一sql语句

liuyude 2003-10-22 03:49:57
表中有三个字段t(f1,f2,f3)
f1 f2 f3
1 a 0
1 b 1
2 c 0
3 d 1
4 e 0
5 g 2
表的内容象上面这样。
现在我要得到:
f3=0的所有行
再加上f3=1并且f1不在f3=0中的所有行。
(换句话说,要是f3=0的记录中出现过的f1,不需要再出现)
结果就是:
1 a 0
2 c 0
3 d 1
4 e 0

...全文
37 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
beckhambobo 2003-10-22
  • 打赏
  • 举报
回复
select * from tbname where f3=0
union
select * from tbname a where f3=1 and not exists(select 1 from tbname b where a.f1=b.f1 and f3=0)
dragonl3 2003-10-22
  • 打赏
  • 举报
回复
不用UNION也可以:
select * from tbname
where f3=0 or f3=1 and f1 not in (
select f1 from tbname where f3=0);
pengdali 2003-10-22
  • 打赏
  • 举报
回复
create table t(f1 int,f2 varchar2(10),f3 int);

insert into t values(1, 'a' , 0);
insert into t values(1, 'b' , 1);
insert into t values(2, 'c' , 0);
insert into t values(3, 'd' , 1);
insert into t values(4, 'e' , 0);
insert into t values(5, 'g', 2);
/

select * from t tem where f3=(select min(f3) from t where f3<2 and f1=tem.f1);
/

F1 F2 F3
---------- ---------- ----------
1 a 0
2 c 0
3 d 1
4 e 0

已选择4行。
pengdali 2003-10-22
  • 打赏
  • 举报
回复
select * from t tem where f3=(select min(f3) from t where f3<2 and f1=tem.f1);
pengdali 2003-10-22
  • 打赏
  • 举报
回复
select * from 表 tem where f3=(select min(f3) from 表 where f1=tem.f1)
bzszp 2003-10-22
  • 打赏
  • 举报
回复
select * from tbname where f3=0
union
select * from tbname where f3=1 and f1 not in (select f1 from tbname where f3=0);

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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