SQL查询A表有数据,而B表没数据
准备表和测试数据
create table A(
id varchar2(10),
name varchar2(20)
);
create table B(
id varchar2(10),
age number
);
insert into A values (101,'zs');
insert into A values (102,'ls');
insert into A values (103,'ww');
insert into B values (101,20);
insert into B values (106,30);
insert into B values (104,23);
需求:选出A有,B没有的ID
方式1:遍历B表的所有ID
select id from A where id not in (select id from B)
方式2:左连接
- 左表中没有和右表匹配的,右表的值会以null填充
select A.*,B.* from A left join B on A.id=B.id where b.id is null