简体   繁体   English

Oracle LEFT OUTER JOIN的问题

[英]Trouble with Oracle LEFT OUTER JOIN

In our program, we have a Many-To-Many relation between a table 'Person' and a table 'Phonem'. 在我们的程序中,表“ Person”和表“ Phonem”之间存在多对多关系。 This is archieved by an join table, 'PersonPhon'. 这由联接表“ PersonPhon”归档。

Person: 人:

PERSID 
NAME
FIRSTNAME
DATEOFBIRTH
TYPE
[...]

PersonPhon: 人Phon:

PEPHPERSID <-- references the person
PEPHPHONID <-- references the phonem

Phonem: 音素:

PHONID
PHONEM

Due to a bug, some recent Persons were persisted without a reference to their respective Phonem entries. 由于存在错误,某些“最近的人员”被持久保存,而未引用其各自的Phonem条目。 To read them from the database, I created a statement: 为了从数据库中读取它们,我创建了一条语句:

select p.persid from    
Person p left outer join PersonPhon ph 
on p.persid = ph.pephpersid              
where p.type = 'natural'

This statement was meant to give me all Persons with no respective entry in PersonPhon, and, with test data (only Persons without PersonPhon-entries) this works well. 该声明旨在为我提供在PersonPhon中没有相应条目的所有人员,并且使用测试数据(仅包含没有PersonPhon条目的人员)可以很好地工作。 However, the statement also selects Persons if they have PersonPhon-entries, so I guess my statement has an error, yet I cannot figure out whats wrong. 但是,该语句还会选择具有PersonPhon条目的“人员”,因此我想我的语句有错误,但是我无法弄清楚出了什么问题。

Edit: 编辑:

Entries: 参赛作品:

Person-Table 人表

PERSID | NAME | FIRSTNAME | AGE | TYPE (other columns ommitted)
76257713 | Wilko | Roger| 30 | natural
76257714 | Martian | Marvin | 50 | natural

PersonPhon-Table 人桌

PEPHPERSID | PEPHPHONID
76257713 | 21000
76257713 | 26000    

Phonem-Table 音位表

PHONID | PHONEM
21000 | 4875122
26000 | 7468112
most entries omitted (> 100000)

With the above data, the statement gives: 利用以上数据,该语句给出:

76257713 
76257713 
76257714 

Which is unexpected behaviour. 这是意料之外的行为。 Expected would be just 76257714 . 预期仅为76257714

If the PersonPhonem-Table is empty, it gives: 如果PersonPhonem-Table为空,则给出:

76257713 
76257714 

Which looks like the expected behaviour, but seems to be misleading. 看起来像预期的行为,但似乎具有误导性。

If you are trying to find rows in person that don't have an entry in another table, it sounds like you want a not exists rather than a left outer join . 如果您试图person查找在另一个表中没有条目的行,这听起来像您想要一个not exists而不是一个left outer join You could do a left outer join and then have a where clause that looks to see that the left joined table's values are null but that is generally less readable. 您可以执行left outer join联接,然后使用where子句,以查看左联接表的值为null但通常可读性较低。

My guess is that you want 我的猜测是你想要

SELECT *
  FROM person p
 WHERE NOT EXISTS( SELECT 1
                     FROM PersonPhon ph
                    WHERE p.persid = ph.pephpersid )
   AND <<your additional predicates>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM