create table a (username varchar2(20));
insert into a values('gaurav');
insert into a values('rahul');
insert into a values('rohan');
insert into a values('niharika');
create table b (username varchar2(20));
insert into b values('gaurav');
insert into b values('rahul');
案例1:当表B中没有记录时
Delete from b;
WITH cnt_b as
(
select count(*) cnt from b
)
select a.username from a ,cnt_b where 1= CASE WHEN cnt_b.cnt=0 THEN 1 ELSE 0 END
UNION
select a.username from a,cnt_b,b where a.username =b.username and cnt_b.cnt>0;
USERNAME
gaurav
niharika
rahul
rohan
4 rows selected.
案例2当表B中有两条匹配记录时
insert into b values('gaurav');
insert into b values('rahul');
WITH cnt_b as
(
select count(*) cnt from b
)
select a.username from a ,cnt_b where 1= CASE WHEN cnt_b.cnt=0 THEN 1 ELSE 0 END
UNION
select a.username from a,cnt_b,b where a.username =b.username and cnt_b.cnt>0;
USERNAME
--------------------
gaurav
rahul
2 rows selected.
Case3当表 B 中有记录,但没有匹配的记录时。
delete from b
insert into b values('rajat');
insert into b values('garima');
with cnt_b as
(
select count(*) cnt from b
)
select a.username from a ,cnt_b where 1= CASE WHEN cnt_b.cnt=0 THEN 1 ELSE 0 END
UNION
select a.username from a,cnt_b,b where a.username =b.username and cnt_b.cnt>0
no rows selected.
干杯 GS