简体   繁体   English

Oracle,LEFT OUTER JOIN不返回左表中的所有行,而是表现得像INNER JOIN

[英]Oracle, LEFT OUTER JOIN not returning all rows from left table, instead behaving like INNER JOIN

I'm doing a left outer join and only getting back matching rows like it was an inner join. 我正在做一个左外连接,只返回匹配的行,就像它是一个内连接。

To simplify the data, my first table(ROW_SEG), or left table looks something like this: 为了简化数据,我的第一个表(ROW_SEG)或左表看起来像这样:

ASN | DEPT NO
-----------------------
85  | 836
86  | null         
87  | null  

My second table(RF_MERCHANT_ORG) has DEPT_NAME, and some other things which i want to get when i have a dept number. 我的第二个表(RF_MERCHANT_ORG)有DEPT_NAME,还有其他一些东西,当我有一个dept号时我想得到它。

DEPT NO | DEPT_NAME
-----------------------
836     | some dept name 1
837     | some dept name 2
838     | some dept name 3

In this case after my join i'd only get 1 row, for ASN 85 that had a DEPT NO. 在这种情况下,在我加入之后我只得到1排,因为ASN 85有DEPT NO。

...omitting a bunch of SQL for simplicity

, ROW_SEG AS (
    SELECT *
    FROM VE_SI_EC_OI
    WHERE ROW_NUM BETWEEN 1 AND 1000 -- screen pagination, hardcoding values
)

-- ROW_SEG has a count of 1000 now

, RFS_JOIN AS (
    SELECT ROW_SEG.*
    ,MO.BYR_NO
    ,MO.BYR_NAME
    ,MO.DEPT_NAME
    FROM ROW_SEG
    LEFT OUTER JOIN RF_MERCHANT_ORG MO
    ON ROW_SEG.DEPT_NO = MO.DEPT_NO    
    WHERE MO.ORG_NO = 100  
)
SELECT * FROM RFS_JOIN; -- returns less than 1000

I only get back the number of rows equal to the number of rows that have dept nos. 我只返回等于具有dept nos的行数的行数。 So in my little data example above i would only get 1 row for ASN 85, but i want all rows with BYR_NO, BYR_NAME, AND DEPT_NAME populated on rows where i had a DEPT_NO, and if not, then empty/null columns. 所以在我上面的小数据示例中,我只会获得ASN 85的1行,但我希望所有行BYR_NO,BYR_NAME和DEPT_NAME都填充在我有DEPT_NO的行上,如果没有,则填充空/空列。

If ORG_NO is within the RF_MERCHANT_ORG table (using aliases consistently would help there) then acting like an inner join would then would be the correct result for the SQL being used. 如果ORG_NO在RF_MERCHANT_ORG表中(一致地使用别名会有帮助),那么就像内部联接一样,将是正在使用的SQL的正确结果。

The join should be this to make it act like a proper left join: 连接应该是这样使它像一个适当的左连接:

LEFT OUTER JOIN RF_MERCHANT_ORG MO ON ROW_SEG.DEPT_NO = MO.DEPT_NO AND MO.ORG_NO = 100

如果ORG_NO在RF_MERCHANGE_ORG中,则可能是原因...... where条件限制了结果集。

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

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