gpt4 book ai didi

sql - 在 Oracle 中使用 IN 运算符进行外连接的解决方法

转载 作者:行者123 更新时间:2023-12-04 22:49:33 25 4
gpt4 key购买 nike

我使用的是 Oracle SQL,因此外连接具有很好的 (+) 语法。我应该警告你,我不允许重新设计数据库;我在一家大型组织工作。

以下是一些示例表:

People
PersonID Name
1 Elmo
2 Oscar
3 Chris

Attribute
PersonID Attribute
1 Happy
1 Muppet
1 Popular
2 Grouchy
2 Muppet
2 Popular
3 Programmer

我想要一个人的名单,我想知道我们是否知道他们是快乐还是不高兴。以下是我想要的输出:
Name       Mood
Elmo Happy
Oscar Grouchy
Chris

所以这是我认为我会使用的查询:
SELECT p.Name, a.Attribute
FROM People p, Attributes a
WHERE p.PersonID = a.PersonID (+)
AND ( a.Attribute (+) = 'Happy'
OR a.Attribute (+) = 'Grouchy' )

(也许我必须输入“OR a.Attribute IS NULL”或其他内容。)

但实际上我根本不允许在外连接中使用 OR !我应该怎么做呢?

最佳答案

首先,为什么不能使用正确的OUTER JOIN s?,您可以在 Oracle 中使用它们而无需与 (+) 进行隐式连接。句法。至于你的问题,可以用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')

关于sql - 在 Oracle 中使用 IN 运算符进行外连接的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10758526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com