发出LEFT JOIN两次,(乘以行)

时间:2014-05-27 16:06:53

标签: sql join left-join

我们假设我现在有3个表,一个表lessons,一个表包含ratings这些课程,另一个表包含users个表课程。

Lessons是一个非常规则的表,其他2个表是这样的关系表:

TABLE LESSONS
ID | NAME | DESCRIPTION | CREATED BY | APPROVED BY | LEVEL | DATE CREATED | LAST EDIT
1    les1    desc1         10           12            1      12-12-2000     12-12-2000
2    les2    desc2         23           12            2      12-12-2000     12-12-2000
3    les3    desc3         12           12            3      12-12-2000     12-12-2000

TABLE RATINGS
ID | LESSON | USER | RATING | COMMENT
1     1        60      5      very good
2     2        30      4      nice
3     2        62      4      my comment
4     3        65      3      nice
5     3        78      5      very good
6     1        26      1      very bad
6     1        45      3      other comment

TABLE LESSONSXUSERS
ID | LESSON | USER | STATUS
1     1        60     2
2     1        26     2
2     1        45     2
3     2        30     2
4     2        62     2
5     3        65     2
6     3        78     2
7     1        22     1
8     1        19     1

我正在尝试生成一个视图,向我显示仅批准的课程,其中一些信息从其他两个表中提取:

CREATE OR REPLACE VIEW `skn_approved_lessons` AS 
select  
 `l`.`id_skn_lessons` AS `id_skn_lessons`,
 `l`.`name` AS `name`,
 `l`.`description` AS `description`,
 `l`.`createdBy` AS `createdBy`,
 `l`.`approvedBy` AS `approvedBy`,
 `l`.`id_skn_lessonsLevels` AS `id_skn_lessonsLevels`,
 `l`.`dateCreated` AS `dateCreated`,
 `l`.`lastEdit` AS `lastEdit`,
 AVG(`lr`.`rating`) AS `avgScore`,
 COUNT(`lxu`.`id_skn_users`) AS `students`
from ((`skn_lessons` AS `l` 
left join `skn_lessonsRatings` AS `lr` on `l`.`id_skn_lessons` = `lr`.`id_skn_lessons`) left join `skn_lessonsXusers` AS `lxu` on `lxu`.`id_skn_lessons` = `l`.`id_skn_lessons`)
where ((`l`.`approvedBy` is not null) and
    (`l`.`approvedBy` <> `l`.`createdBy`))
group by `l`.`id_skn_lessons`;

它&#34;有点&#34;工作,因为它告诉我我想要什么,但用户数量是错误的。我知道为什么会这样,但我不知道如何解决它。

问题是,我应该得到这个:

VIEW APPROVED
ID | NAME | DESCRIPTION | CREATED BY | APPROVED BY | LEVEL | DATE CREATED | LAST EDIT  | AVG RATING | STUDENTS
1    les1    desc1         10           12            1      12-12-2000     12-12-2000       3          4
2    les2    desc2         23           12            2      12-12-2000     12-12-2000       4          2
3    les3    desc3         12           12            3      12-12-2000     12-12-2000       4          2

但我得到了这个:

VIEW APPROVED
ID | NAME | DESCRIPTION | CREATED BY | APPROVED BY | LEVEL | DATE CREATED | LAST EDIT  | AVG RATING | STUDENTS
1    les1    desc1         10           12            1      12-12-2000     12-12-2000       3          15
2    les2    desc2         23           12            2      12-12-2000     12-12-2000       4          4
3    les3    desc3         12           12            3      12-12-2000     12-12-2000       4          4

注意用户列是错误的,我真正得到的是users x amountOfRatings的产品。它由user的每个注册表向rating的每个注册表进行查询,因此我总是得到users x ratings作为金额users,这不是我想要的。

我不太清楚在lesson.id分组之后如何进行第二次加入,我想这将解决问题。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我相信您需要做的就是将DISTINCT参数添加到COUNT聚合函数中,如下所示:

COUNT(DISTINCT lxu.id_skn_users)

添加DISTINCT参数将返回该列中唯一的非空值的数量。

我看到Barmar已将其纳入上述评论中。