2014-11-04 140 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