一个简单的SQL问题(关于oracle的select 语句)

polokang 2005-11-02 11:35:06
表A 和表B 同有一个相同的字段name;
现在需要根据name字段将A表中有而B表中却没有的数据给查找出来,请问这个语句怎么写啊?
我写的如下的语句,一运行就停不下来了!
select * from A,B where A.name != B.name
...全文
106 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzwind5 2005-11-03
  • 打赏
  • 举报
回复
楼上的看来你没有真正理解 exists 和 not exists 的用法
select * from a where not exists (select * from b where a.col1=b.col1);
等同于
select * from a where a.col1 not in (select b.col1 from b );
zzwind5 2005-11-02
  • 打赏
  • 举报
回复
select * from a where not exists (select * from b where a.name=b.name)
也能用
超叔csdn 2005-11-02
  • 打赏
  • 举报
回复
你这样查不到你想要的结果.
那样会导致查询到很多很多的记录!
建议改成
select * from a
where a.name not in (select b.name from b
where b.name is not null)
shenzhenzsf 2005-11-02
  • 打赏
  • 举报
回复
zzwind5() 兄:
连接的结果有三种情况:a表存在,b表不存在;a表存在,b表存在;a表不存在,b表存在。你的语句多了一种a表没有,b表有的情况。
zzwind5 2005-11-02
  • 打赏
  • 举报
回复
SQL> select * from a;

COL1
----------
1
2
3
4
5
6
7
8
9
10

10 rows selected.

SQL> select * from b;

COL1
----------
1
2
3
4
5

SQL> select * from a where not exists (select * from b where a.col1=b.col1);

COL1
----------
6
7
8
9
10

不是要上面的结果???
polokang 2005-11-02
  • 打赏
  • 举报
回复
谢谢两位的帮助了!
polokang 2005-11-02
  • 打赏
  • 举报
回复
不好意思,是我搞错了,没有 distinct!
我的SQL: select distinct name from a where name not in(select distinct name from b);
谢谢两位的帮助了!
polokang 2005-11-02
  • 打赏
  • 举报
回复
同志们,好象还是不对吗! 查询还是有 N 多条,结果还是不对!

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧