Workaround for outer join with an IN operator in Oracle

后端 未结 3 1619
萌比男神i
萌比男神i 2021-01-13 16:18

I am using Oracle SQL, so outer joins have the nice (+) syntax. I should warn you that I am not allowed to redesign the database; I work for a large organization.

He

相关标签:
3条回答
  • 2021-01-13 16:51

    If you really know the Oracel SQL syntax for a "proper" Oracle database, you could also do this...

    SELECT p.Name,
           a.Attribute
      FROM people p,
           (SELECT PersonID,
                   Attribute
              FROM attributes
                  WHERE Attribute = 'Happy'
                  OR Attribute = 'Grouchy') a
      WHERE p.personid = a.personid(+)
    

    The point being that ANSI vs Oracle syntax is a ridiculous comment. Oracle supports both, you whichever is easier/better/manageable for you.

    0 讨论(0)
  • 2021-01-13 16:54

    First of all, why can't you use proper OUTER JOINs?, you can use them in Oracle without having to do the implicit joins with the (+) syntax. As for your problem, you can use IN:

    SELECT p.Name, a.Attribute
    FROM People p
    LEFT OUTER JOIN Attributes a
    ON p.PersonID = a.PersonID AND a.Attribute IN ('Happy','Grouchy')
    
    0 讨论(0)
  • 2021-01-13 17:12

    Sorry to answer my own question. To avoid the error ORA-01719, I changed everything to "proper" joins at the advice of @Lamak, and then went with this solution:

    SELECT p.Name, a.Attribute
    FROM People p
    LEFT OUTER JOIN  (SELECT PersonID, Attribute
                      FROM Attributes
                      WHERE Attribute = 'Happy' OR Attribute = 'Grouchy') a
    ON (p.PersonID = a.PersonID)
    
    0 讨论(0)
提交回复
热议问题