如何查询表A中的某字段的值在表B中不存在?

我有两张表A,B , 两张表都有number字段 ,但内容只有一部分是相同的。请问我该如何查询B中所有在A中不存在的number ?

用not in语句来实现。

1、测试表创建,插入数据:

create table a
(id int,
name varchar2(10));

create table b
(id int);

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

insert into b values (1);
insert into b values (3);

2、查询b表中存在的id不出现在a表中的内容,可用语句:

select * from a where id not in (select id from b);

3、结果:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-02-05

1、创建测试表,

create table test_tbl_a(num number);

create table test_tbl_b(num number);

2、插入测试数据;

insert into test_tbl_a values (1);

insert into test_tbl_a values (2);

insert into test_tbl_b values (1);

insert into test_tbl_b values (2);

insert into test_tbl_b values (3);

insert into test_tbl_b values (4);

commit;

3、查询B表中全量数据;select t.*, rowid from test_tbl_b t;

4、编写语句,查询B中所有在A中不存在的number;

   select t.* from test_tbl_b t where not exists (select 1 from test_tbl_a b where t.num = b.num)

本回答被网友采纳
第2个回答  推荐于2017-09-25
select * from B where number not in (select number from A)

select B.* from B left join A on A.numer=B.number
where A.number is null本回答被提问者采纳
第3个回答  2011-08-10
select number from b where not number in (select number from a)

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网