简体   繁体   English

多表外连接问题

[英]Multiple table Outer Join problem

I may have have the wrong end of the stick here, but I thought that an outer join was supposed to give me all the records that were in either the (say) left table, along with the matching results from the right table, and nulls where there was no match. 我在这里可能遇到了错误的情况,但是我认为应该使用外部联接为我(例如)左表中的所有记录,以及右表中的匹配结果,以及null没有比赛的地方。

I have 3 tables I need to query. 我有3个表需要查询。 Person and Detail have a direct 1:1 relationship. 人与细节具有直接的1:1关系。 The third table is Time. 第三个表是时间。 This stores hours worked on a weekly basis, per person, per project. 该库存储每个人每个项目每周工作的小时数。 I need a count of the number of weeks each person has put down to. 我需要计算每个人的工作周数。

The following gives me the number of weeks that each user has put time down to as long as they have put down > 1 week 以下是我给每个用户放下超过1周的时间的周数

SELECT name, detail.clock, COUNT(DISTINCT(week))
FROM person, detail,     
WHERE person.ref = detail.person
AND detail.clock = time.clock
WHERE time.week >= "2010-07-01" 
GROUP BY detail.clock

ie this will show the 80/100 people who have entered time. 即,这将显示80/100输入时间的人。

However I need to see those 20 people who have not yet put time down, so I tried the following outer join 但是,我需要看到尚未放下时间的这20个人,所以我尝试了以下外部联接

SELECT name, detail.clock, COUNT(DISTINCT(week))
FROM person LEFT OUTER JOIN detail ON person.ref = detail.person
LEFT OUTER JOIN time ON detail.clock = time.clock
WHERE time.week >= "2010-07-01" 
GROUP BY detail.clock

However this gives me exactly the same result as the first query. 但是,这给了我与第一个查询完全相同的结果。

EDIT: I've just discovered part of the problem. 编辑:我刚刚发现问题的一部分。 In the original version of the pseudo code there was no flittering by date. 在伪代码的原始版本中,按日期没有晃动。 I've just found that removing the date filter the query behaves more as expected - albeit too slow to be useful, and the date is important. 我刚刚发现,删除日期过滤器后,查询的行为将达到预期的效果-尽管速度太慢,无法使用,而且日期很重要。

查阅Jeff Atwood的JOIN视觉指南 ,看看是否可以使自己专心。

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

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