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