有两张表:tmp_dzfwq、tmp_serarea都有地址id(stid)这个字段
select distinct stid from tmp_dzfwq --581条记录
select distinct stid from tmp_serarea --1022条记录两张表里有一部分stid值是相同的,我现在想找出在serarea表里有的stid,在dzfwq表里没有的stid:
select distinct stid from tmp_serarea s
where exists(select 1 from tmp_dzfwq z where s.stid<>z.stid) --1022条记录
这样的话居然还是把serarea表里的所有stid都查出来了;如果这样:
select distinct stid from tmp_serarea s
where not exists(select 1 from tmp_dzfwq z where s.stid=z.stid) --467条记录为什么这样写跟上面的差别这么大?我觉得这两条语句要表达的意思是一样的啊!!还有要找出serarea表里有的stid,在dzfwq表里没有的stid下面的语句对不对?
select distinct stid from tmp_dzfwq --581条记录
select distinct stid from tmp_serarea --1022条记录两张表里有一部分stid值是相同的,我现在想找出在serarea表里有的stid,在dzfwq表里没有的stid:
select distinct stid from tmp_serarea s
where exists(select 1 from tmp_dzfwq z where s.stid<>z.stid) --1022条记录
这样的话居然还是把serarea表里的所有stid都查出来了;如果这样:
select distinct stid from tmp_serarea s
where not exists(select 1 from tmp_dzfwq z where s.stid=z.stid) --467条记录为什么这样写跟上面的差别这么大?我觉得这两条语句要表达的意思是一样的啊!!还有要找出serarea表里有的stid,在dzfwq表里没有的stid下面的语句对不对?
解决方案 »
- oracle process和sga有关系吗?
- oracle的查询语句,在线等,急!!
- 如何数据类型INT 改成 VARCHAR?
- 求ORACLE管理方面的基础入门书籍,100分不够再加!!!!!!
- 超菜问题,如何指定条件更新一条记录
- 如何将schema保存成.sql文件,以后可以用来恢复数据库。
- CSDN上各位兄弟,看看OracleServiceXXXX服务会自动停止,实在是找不出原因,快急疯了!!!
- 如何嵌套调用返回游标的存储过程
- 在写存储过程时,有没有什么方法快速将表中的所有字段名加上P_
- 删除表空间的问题
- oracle读取xml的信息问题
- 求一ORACLE取最大一条数据的SQL(重分)
select distinct stid from tmp_serarea s
where exists(select 1 from tmp_dzfwq z where s.stid<>z.stid) --1022条记录select distinct stid from tmp_serarea s
where not exists(select 1 from tmp_dzfwq z where s.stid=z.stid) --467条记录
sql1和sql2可不等价。
sql1:表tmp_serarea每检索一条记录后,到表tmp_dzfwq判断,满足s.stid<>z.stid的时候(基本都满足),返回true.所以返回 1022了
tmp_serarea stid 1,2,3
sql1:当tmp_dzfwq stid=1 select 2,3;当tmp_dzfwq stid=2 select 1,3;当tmp_dzfwq stid=3 select 1,2
最后 1,2,3sql2:当tmp_dzfwq stid=1 select 无记录;tid=2 select 无记录;tid=3 select 无记录(因为tmp_serarea均存在)
最后 无记录
select distinct stid from tmp_serarea s
where exists(select 1 from tmp_dzfwq z where s.stid<>z.stid) --1022条记录select distinct stid from tmp_serarea s
where not exists(select 1 from tmp_dzfwq z where s.stid=z.stid) --467条记录--举例:tmp_serarea 两条记录 1,2,tmp_dzfwq 一条记录 1,3
--第一种写法: 对于 1 这个记录,存在 3 与他不等,而条件是exists 因此查出
--第二中写法: 对于 1 这个记录,存在 1 与他相等,而条件是not exists 因此查不出。
select distinct stid from tmp_serarea s
where exists(select 1 from tmp_dzfwq z where s.stid=z.stid) select distinct stid from tmp_serarea s
where not exists(select 1 from tmp_dzfwq z where s.stid<>z.stid)
第一个是找出在两张表里都存在的stid
第二个是找出serarea表里的stid,条件是在dzfwq表里找不到跟该stid不等的记录,结果当然是0感谢各位,终于理解了!!