not in/not exists不好使了?
我有两个表
表A:有字段ID,Name...
ID Name
1 aa
2 bb
3 cc
6 dd
7 ee
表B:有字段ID,Addr...
ID Addr
1 f
2 g
3 h
4 i
5 j
需要查找出A中ID不存在于B中ID的记录,需要的结果如下:
ID Name
6 dd
7 ee
我的写法如下:
select * from A where id not in (select id from B)
select * from A where not exists (select id from B)
但是两种写法查出来的记录都为空。
强调一下,上面的只是我举的简单例子,我要实现的功能和上面类似
我把那两个表的一部分内容手动输到自己另外建的2个表作试验,都是可以查出来的
可就是我要用的那两个表查不出来,问了几个人都说语法没问题
请问有什么原因造成这种情况?
我的数据库是9i,两个表的数据类型都是varchar2。
一解决问题马上给分
谢谢。
问题点数:100、回复次数:5Top
1 楼pipiright(pipiright)回复于 2005-02-24 02:56:57 得分 0
请顺便把能实现上述功能的所有SQL语句写法写出来
谢谢。Top
2 楼pipiright(pipiright)回复于 2005-02-24 03:00:07 得分 0
奇怪的是in和exists是可以查出表A中ID在表B中存在的记录的Top
3 楼lee_billiy(思思)回复于 2005-02-24 08:29:17 得分 70
你的例子的写法应该是select * from A where id not in (select id from B where A.id = B.id)
select * from A where not exists (select id from B where A.id = B.id)
Top
4 楼bzszp(SongZip)回复于 2005-02-24 08:54:19 得分 0
第一条语句没看出有问题。
SQL> select * from a where id not in(select id from b);
ID NAME
---------- ----------
6 dd
7 ee
SQL> select * from a where not exists(select 1 from b where b.id=a.id);
ID NAME
---------- ----------
6 dd
7 ee
SQL>Top
5 楼jiangchuandong(岁月的流逝......)回复于 2005-02-24 10:07:40 得分 30
如果a,b的表结构相同的话才可以这样写
select * from A where not exists (select * from B)
Top




