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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM