请各位大侠给小弟参考个sqla表和b表的表结构一样,a和b通过id关联,我如何取出a和b关联不上的数据,如果写出来,最好解释一下,谢谢!在线等
解决方案 »
- 关于一个SQL语句,困惑了,求指导
- 求助:window2008x64 oracle 11G 通过dblink 访问 hpux oracle 10G 速度极慢
- about autotrace question
- 请问:怎样在oracle中导入access表格?
- 救火,Oracle中查询系统时间是From那张表呀?
- 怎么将数据插入到 oracle92中表分区中呢?
- ORACEL805的Schema和Storage Manager到了9i是什么?
- 想学习Oracle不知道从那入手!对Oracle很迷茫! 大家谈谈,进者有分
- 怎么用一条语句完成?
- 类型转换?谢谢
- 我采用For loop的游标循环方式进行表的记录插入,发现游标读不到记录
- ORACLE sql 优化 运行速度太慢 要40秒 慢在 sum()块
select * from a
where not exists(select id from b where a.id=b.id)--a中存在,b中不存在
union all--合并两个结果,即为a,b关联不上的
select * from b
where not exists(select id from a where a.id=b.id);--b中存在,a中不存在
1.取得存在a但是不存在b的数据
select * from a
minus
select * from b;2.取得存在b但是不存在a的数据
select * from b
minus
select * from a;
aid is null or bid is null
假设A表是主表,并且A.ID和B.ID都不为空,上面高手说过的不再重复.作用是从A表删除ID等于B.ID的记录:
参考:SELECT
A.COL1
,A.COL2
...
,A.COLN
FROM TABLEA A, TABLEB B
WHERE 1=1
AND A.ID = B.ID(+)
AND B.ID IS NULL
;
Sorry,理解漏了你的意思,上面的方式并不能将B中不存在于A的记录筛选出来.你只需要把顺序互换再union all就行.
1.not exists+union all (注意union all两个数据集出来的列名(不相同可用别名)及类型要相同,这儿只输出id了)
select id from a where not exist(select 1 from b where b.id=a.id) union all select id from b where not exists(select 1 from a where a.id=b.id)
2.full outer join+where is null(即全表连接,取出关联不到其他表数据项即为需要的) select * from (select a.id as aid,b.* from a full outer join b on b.id=a.id where a.id is null or b.id is null)c