多表连接 - 我可以添加一个外部连接吗?
我在从外部联接有效的情况转移到失败的情况时遇到问题。
工作(伪代码示例)
SELECT a.number, a.name, b.ref, c.ref, c.firmref
FROM jobs a, teams b LEFT OUTER JOIN teamfirms c ON b.ref = c.team
WHERE a.ref = b.job
工作和团队之间存在多对一关系(每个工作有多个团队),并且总是填充
表 c 中可能有也可能没有行,但上面的查询给出了我期望的结果(大约 5000 条记录)
当我想从第四个表中引入有关团队的详细信息时,问题就出现了
我正在尝试的代码如下
SELECT a.number, a.name, b.ref, c.ref, c.firmref, d.name
FROM jobs a, teams b LEFT OUTER JOIN teamfirms c ON b.ref = c.team, firms d
WHERE a.ref = b.job
AND d.ref = c.firmref
此时,我试图捕获的 NULL 消失了,我删除大约 500 条记录
我做错了什么?
I'm having a problem moving from a situation where an Outer Join works, to where it fails.
Working (pseudo code example)
SELECT a.number, a.name, b.ref, c.ref, c.firmref
FROM jobs a, teams b LEFT OUTER JOIN teamfirms c ON b.ref = c.team
WHERE a.ref = b.job
There is a many to one relationship between jobs and teams (many teams per job) that is always populated
There may or may not be firms in table c, but the query above gives me the result I would expect (approx 5000 records)
The problem comes when I want to bring in the details about the teams from a fourth table
The code I am trying is below
SELECT a.number, a.name, b.ref, c.ref, c.firmref, d.name
FROM jobs a, teams b LEFT OUTER JOIN teamfirms c ON b.ref = c.team, firms d
WHERE a.ref = b.job
AND d.ref = c.firmref
At this point the NULLS that I am trying to capture disappear and I drop approx 500 records
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对此进行打击。
或者你可以选择
其中之一...而不是两者都做。
只是为了更好地衡量这一点......
take a whack at this.
or you could do
one or the other... not both.
Just to throw this in for good measure...
您正在混合 ANSI 89 和 92 JOIN 语法(隐式和显式 JOIN)。尝试将整个查询转换为显式联接。问题可能是您添加的新 JOIN(隐式语法)是 INNER 并且想要成为 OUTER,或者您想要以不同的顺序解析 JOIN(一旦将它们全部写为 OUTER,您可以使用括号来完成此操作)加入)
You are mixing ANSI 89 and 92 JOIN syntax (implicit and explicit JOINs). Try converting the entire query to explicit JOINs. The problem is likely that the new JOIN you're adding (implicit syntax) is INNER and wants to be OUTER, or that you want to resolve the JOINs in a different order (which you can do with parens once you write them all as OUTER JOINs)
尝试以下操作:
如果有效,您可以尝试将第二个 LEFT OUTER 变成 INNER。当我需要这类东西时,我通常把它作为外部,这可能是错误的。
Try, the following:
If it works, you could then try to turn the 2nd LEFT OUTER into an INNER. Possibly incorrectly I've generally left it as an outer when I've needed this sort of thing.
这是我的尝试:
这会将所有工作加入团队,如果存在团队公司,那么还会带来公司详细信息。如果没有团队牢固的关系,你仍然会得到空值。
Here is my attempt:
This will join all jobs to team and if a teamfirm exist then also bring firm details. if no team firm relationship you still get your nulls.
请尝试以下操作:
Try the following: