2014-11-04 141 views
1

我知道这很简单,但我是sql新手,并没有线索如何编写此查询。我有3个表格:rekon,流量和笔记。在sql中加入3个表oracle

rekon:

no sex 
a  F 
b  F 
c  M 
d  F 
e  M 

流量:

no notes_no 
a PX 
a SX 
a  
a DX 
b MX 
b CX 
c  
c PX 
d LX 
d WX 

笔记:

notes_no no 
AX  a 
BX  f 
CX  g 
DX  a 
EX  c 
FX  c 
GX  g 
HX  b 
PX  a 
SX  a 

我想列出从笔记中不流动的一部分,只有来自客户端的所有条目, rekon是女性。所以结果应该是

no sex notes_no 
a F  AX 
b F  HX 

我尝试使用的语句

notes.notes_no not in (select distinct notes_no from flows) 

但我有很多行和Oracle有问题,计算它。

预先感谢您。

+0

那么,真正的概率。是? – 2014-11-04 10:24:58

回答

0

尝试此查询:

SELECT notes.* FROM notes 
INNER JOIN rekon ON rekon.no = notes.no 
LEFT JOIN flows ON flows.no = notes.no 
WHERE flows.no IS NULL and sex = 'F' 
0
select N.* 
from rekon R 
    join notes N 
     on N.no = R.no 
where R.sex = 'F' 
    and not exists (
     select 1 
     from flows F 
     where F.notes_no = N.notes_no 
    ) 
; 

查询可能会根据您的表间的参照关系的定义略有不同,所以exists谓语不妨读

and not exists (
     select 1 
     from flows F 
     where F.no = N.no 
      and F.notes_no = N.notes_no 
    ) 

如果Oracle有查询的数据问题合理的时间,那么你可能

  1. 缺乏统计表上的统计数据(...所以甲骨文挑错了执行计划)
  2. 是具有严重的组织表(......所以Oracle做的无用功不必要的量)
  3. 是具有错误配置的Oracle服务器(...所以它的斗争中哈希联接)
  4. 是具有太慢服务器marchine