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に一致するレコードが2つある場合
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