在SQL中的表之间连接

时间:2016-06-22 11:50:28

标签: mysql sql join inner-join

我有5张桌子

1. SCHOOL[id(bigInt, primary), name(varchar)]
2. SELECTED_INDICATOR[id(bigInt, primary), school_id(bigint)]
3. TEACHER[id(bigint, primary), indicator_id(bigInt), attendance_id(int)]
4. STUDENT[id(bigint, primary), indicator_id(bigInt), attendance_id(int)]
5. MIDDAY_MEAL[id(bigint,primary), indicator_id(bigint), served(boolean), consumed_number(int)]

在TEACHER表中,attendance_id可以有值:1或2或3。 同样,在STUDENT表中,attendance_id可以具有值:1或2。

我必须根据SELECTED_INDICATOR id生成报告,格式为:

School_id | School_Name | Total_Teacher |老师_1老师_2老师_3 | Total_Student | Student_1 | Student_2 |服务| consumed_number

为此我尝试过:

select A.id, A.school_id, SC.name, 
 SUM(CASE WHEN T.attendance_id IN (1,2,3) THEN 1 ELSE 0 END) as TOTAL_TEACHER,
 SUM(CASE WHEN T.attendance_id IN (1) THEN 1 ELSE 0 END) as TEACHERS_1,
 SUM(CASE WHEN T.attendance_id IN (2) THEN 1 ELSE 0 END) as TEACHERS_2,
 SUM(CASE WHEN T.attendance_id IN (3) THEN 1 ELSE 0 END) as TEACHERS_3,
 SUM(CASE WHEN S.attendance_id IN (1,2) THEN 1 ELSE 0 END) as TOTAL_STUDENT,
 SUM(CASE WHEN S.attendance_id IN (1) THEN 1 ELSE 0 END) as STUDENTS_1,
 SUM(CASE WHEN S.attendance_id IN (2) THEN 1 ELSE 0 END) as STUDENTS_2,
 M.served, M.consumed_number
from SELECTED_INDICATOR A
join SCHOOL SC on A.school_id = SC.id
join TEACHER T on A.id = T.indicator_id
join STUDENT S on A.id = S.indicator_id
join MIDDAY_MEAL M on A.id = M.indicator_id
WHERE A.STATUS = 'COMPLETED' group by A.id;

当我一次用SELECTED_INDICATOR加入TEACHER或STUDENT时,它会给我正确的数据。但是当我像上面的查询一样用SELECTED_INDICATOR加入TEACHER和STUDENT时,我得到了与教师和学生相关领域的大量数字。

我的查询有什么问题?请帮助纠正它,或提供任何替代查询。

2 个答案:

答案 0 :(得分:1)

尝试使用具有选项的COUNT()来使用区分值。问题是表格会使结果倍增。

    select A.id, A.school_id, SC.name, 
     COUNT(DISTINCT CASE WHEN T.attendance_id IN (1,2,3) THEN t.TeacherID  END) as TOTAL_TEACHER,
     COUNT(DISTINCT CASE WHEN T.attendance_id IN (1) THEN t.TeacherID END) as TEACHERS_1,
    ....
    FROM ....

答案 1 :(得分:0)

    SELECT A.id, A.school_id, SC.name,
    (SELECT COUNT(T.attendance_id) FROM TEACHER T GROUP BY T.indicator_id HAVING A.id = T.indicator_id) AS TOTAL_TEACHER,
    (SELECT COUNT(T.attendance_id) FROM TEACHER T WHERE T.attendance_id = 1 GROUP BY T.indicator_id HAVING A.id = T.indicator_id) AS TEACHERS_1,
    (SELECT COUNT(T.attendance_id) FROM TEACHER T WHERE T.attendance_id = 2 GROUP BY T.indicator_id HAVING A.id = T.indicator_id) AS TEACHERS_2,
    (SELECT COUNT(T.attendance_id) FROM TEACHER T WHERE T.attendance_id = 3 GROUP BY T.indicator_id HAVING A.id = T.indicator_id) AS TEACHERS_3,
    (SELECT COUNT(S.attendance_id) FROM STUDENT S GROUP BY S.indicator_id HAVING A.id = S.indicator_id) AS TOTAL_STUDENT,
    (SELECT COUNT(S.attendance_id) FROM STUDENT S WHERE S.attendance_id = 1 GROUP BY S.indicator_id HAVING A.id = S.indicator_id) AS STUDENTS_1,
    (SELECT COUNT(S.attendance_id) FROM STUDENT S WHERE S.attendance_id = 2 GROUP BY S.indicator_id HAVING A.id = S.indicator_id) AS STUDENTS_2,
    M.served, M.consumed_number
    FROM SELECTED_INDICATOR A
    JOIN SCHOOL SC on A.school_id = SC.id
    JOIN MIDDAY_MEAL M on A.id = M.indicator_id