sql 2个表之间的显示

来源:百度知道 编辑:UC知道 时间:2024/04/27 17:43:44
现有2个表:A表,B表。A表是母表,B表是A的子表即A表内容包含B表内容。我想显示A表中B表没有的内容。怎么写SQL语句?

你的意思是A表是主表,B表是明细表
A与B表是1对多的关系!
如果是这样的话
假设A表与B表 是通过 SID这个字段来 连接的
1.你想查询出B表是否有多余的与A表无关的信息,下面的这条语句。
select * from B where B.SID not in (select A.SID from A )
2.你想查询出A表是否有多余的与B表无关的信息,下面的这条语句
select * from A where A.SID not in (select DISTINCT B.SID from B )

select * from a left join b on a.id=b.id
where b.id is null

把*改成你自己想要的字段就行了

select * from A
where [关联列] not in (select [关联列] from B)

使用联合查询

select * from a;
minus
select * from b;

select A.*,B.* from A
outer join B on
A.id = B.id

假设a表的id和b表的id是关联的
select * from a where a.id not in (select id from b)