将2个SQL查询与公共字段和AVG合并

时间:2014-03-24 23:53:51

标签: mysql sql join merge moodle

我的SQL在没有研究它2年后有点生疏,所以我找不到办法做到这一点。

我需要从Moodle数据库中获取一些数据,即一些平均的反馈测试,学生会对他们的老师进行评分。

此SQL查询:

SELECT mdl_course.id, mdl_user.username FROM mdl_course
INNER JOIN mdl_context ON mdl_context.instanceid = mdl_course.id
INNER JOIN mdl_role_assignments ON mdl_context.id = mdl_role_assignments.contextid
INNER JOIN mdl_role ON mdl_role.id = mdl_role_assignments.roleid
INNER JOIN mdl_user ON mdl_user.id = mdl_role_assignments.userid
WHERE mdl_role.id = 3

返回类似这样的内容,每个课程及其教师的分配表。

COURSE_ID TEACHER   
2          john
3          mary
4          john

现在,我的第二个SQL是这样的:

SELECT mdl_feedback.course, AVG(mdl_feedback_value.value) as average
FROM mdl_feedback_value 
INNER JOIN mdl_feedback_item ON mdl_feedback_value.item = mdl_feedback_item.id 
INNER JOIN mdl_feedback ON mdl_feedback.id = mdl_feedback_item.feedback
INNER JOIN mdl_feedback_completed ON mdl_feedback.id = mdl_feedback_completed.feedback
INNER JOIN mdl_user ON mdl_feedback_completed.userid = mdl_user.id
GROUP BY mdl_feedback.course

COURSE    AVERAGE   
2          3.5
3           3
4          3.25

我想要的是将这两个SQL查询组合成一个这样的,使用COURSE / COURSE_ID作为键

TEACHER    AVERAGE
john         3,375   <--- avg of 3,5 and 3,25 from each of john's courses
mary         3       <--- she has just one course so no math here

我不知道如何去做,所以我很感激一些帮助:)正如我所说,我已经有一段时间没用SQL了所以我不喜欢那些加入的东西,也许我必须使用它们,因为我在这里使用它们。

我正在使用MySQL 5.5.33,虽然这与Moodle有关,但答案并非真正以Moodle为中心,因为这里唯一重要的是哪些表作为两个查询的输出。

由于

3 个答案:

答案 0 :(得分:1)

如果您获取每个子查询然后尝试加入它们,它应该可以工作。我添加了第三个字段,以帮助您查看分数的创建细节。

SELECT username, AVG(average) AS average
  , GROUP_CONCAT(CONCAT('Course: ', teachers.course_id,' with score ', COALESCE(average,'No Score Found'))) AS detail
FROM (
    SELECT mdl_course.id AS course_id, mdl_user.username AS username
    FROM mdl_course
    INNER JOIN mdl_context ON mdl_context.instanceid = mdl_course.id
    INNER JOIN mdl_role_assignments ON mdl_context.id = mdl_role_assignments.contextid
    INNER JOIN mdl_role ON mdl_role.id = mdl_role_assignments.roleid
    INNER JOIN mdl_user ON mdl_user.id = mdl_role_assignments.userid
    WHERE mdl_role.id = 3 ) AS teachers
LEFT JOIN (
    SELECT mdl_feedback.course AS course_id, AVG(mdl_feedback_value.value) as average
    FROM mdl_feedback_value 
    INNER JOIN mdl_feedback_item ON mdl_feedback_value.item = mdl_feedback_item.id 
    INNER JOIN mdl_feedback ON mdl_feedback.id = mdl_feedback_item.feedback
    INNER JOIN mdl_feedback_completed ON mdl_feedback.id = mdl_feedback_completed.feedback
    INNER JOIN mdl_user ON mdl_feedback_completed.userid = mdl_user.id
    GROUP BY mdl_feedback.course) AS scores
  ON teachers.course_id = scores.course_id
GROUP BY username

答案 1 :(得分:0)

我正在尝试使用临时表存储两个不同的查询结果集,然后我查询临时表以获得教师的平均值。希望这有效。

CREATE TABLE #TEMP
(
COURSE_ID int,
TEACHER varchar(100),
COURSE int,
AVERAGE int
)
--- Inserting the course_id and Teahcer data----

INSERT INTO #TEMP
(
COURSE_ID,
TEACHER
)
SELECT mdl_course.id, mdl_user.username FROM mdl_course
INNER JOIN mdl_context ON mdl_context.instanceid = mdl_course.id
INNER JOIN mdl_role_assignments ON mdl_context.id = mdl_role_assignments.contextid
INNER JOIN mdl_role ON mdl_role.id = mdl_role_assignments.roleid
INNER JOIN mdl_user ON mdl_user.id = mdl_role_assignments.userid
WHERE mdl_role.id = 3

-- Inserting the course and average data---
INSERT INTO #TEMP
(
COURSE,
AVERAGE
)
SELECT mdl_feedback.course, AVG(mdl_feedback_value.value) as average
FROM mdl_feedback_value 
INNER JOIN mdl_feedback_item ON mdl_feedback_value.item = mdl_feedback_item.id 
INNER JOIN mdl_feedback ON mdl_feedback.id = mdl_feedback_item.feedback
INNER JOIN mdl_feedback_completed ON mdl_feedback.id = mdl_feedback_completed.feedback
INNER JOIN mdl_user ON mdl_feedback_completed.userid = mdl_user.id
GROUP BY mdl_feedback.course


--- Querying the # temp table for the average and teacher---

SELECT TEACHER, AVG(AVERAGE)
FROM #TEMP
GROUP BY TEACHER

答案 2 :(得分:0)

您可以尝试使用

加入这两个查询
INNER JOIN mdl_course ON mdl_feedback.course = mdl_course.id

请查看此合并查询是否有效:

SELECT mdl_user.username, AVG(mdl_feedback_value.value) as average
FROM mdl_feedback_value 
INNER JOIN mdl_feedback_item ON mdl_feedback_value.item = mdl_feedback_item.id 
INNER JOIN mdl_feedback ON mdl_feedback.id = mdl_feedback_item.feedback
INNER JOIN mdl_feedback_completed ON mdl_feedback.id = mdl_feedback_completed.feedback
INNER JOIN mdl_user ON mdl_feedback_completed.userid = mdl_user.id
INNER JOIN mdl_course ON mdl_feedback.course = mdl_course.id
INNER JOIN mdl_context ON mdl_context.instanceid = mdl_course.id
INNER JOIN mdl_role_assignments ON mdl_context.id = mdl_role_assignments.contextid
INNER JOIN mdl_role ON mdl_role.id = mdl_role_assignments.roleid
INNER JOIN mdl_user ON mdl_user.id = mdl_role_assignments.userid
WHERE mdl_role.id = 3
GROUP BY mdl_user.username;