查两个表中两列都不相等的SQL

查t1表中,列c1和列c2分别不等于t2表的列c1和列c2的记录。
create table T1(
  c1 NUMBER(6),
  c2 NUMBER(6)
);
insert into t1 select rownum, rownum from user_tab_columns where rownum < 100;
update t1 set c2 = c2+50;
select * from t1;
create table T2(
  c1 NUMBER(6),
  c2 NUMBER(6)
);
insert into t2 select rownum, rownum from user_tab_columns where rownum < 100;
update t2 set c1 = c1+50 where c1 > 80;
update t2 set c2 = c2-50 where c2 > 50;
select * from t2;


错误方法:
select t1.* from t1, t2 where t1.c1 != t2.c1 and t1.c2 != t2.c2

 

方法一:
select * from t1
minus
select t1.* from t1, t2 where t1.c1 = t2.c1
minus
select t1.* from t1, t2 where t1.c2 = t2.c2

 

方法二:
select *
  from t1
 where not exists(
       select * from t2 where t1.c1 = t2.c1 or t1.c2 = t2.c2)

你可能感兴趣的:(查两个表中两列都不相等的SQL)