如何在多对多查询中聚合结果

How to aggregate results in many to many Query

我的数据库中有 3 个表:

学生:

Student_Course:

课程:

我想列出所有学生以及他们是否通过了他们选择的所有课程的结果。假设成绩 <= 'C' 为及格。

我试过 sql 喜欢:

SELECT s.*,
IF('C'>=ALL(SELECT c.grade FROM from Course c WHERE c.id=sc.course_id),1,0) as isPass 
FROM Student s LEFT JOIN Student_Course sc on sc.student_id=s.id 

这个sql有效,但是如果现在我想要一个列'isGood',这意味着所有的成绩='A',我需要再次执行子查询吗?如何通过仅执行一次子查询来获得 'isGood' 和 'isPass'?

我相信交界处的成绩会更好table。使用它,我创建了一个可以帮助您解决问题的场景:

场景

create table student (id int, fullname varchar(50));
insert into student values (1, 'john'), (2, 'mary'), (3, 'matt'), (4, 'donald');

create table course (id int, coursename varchar(50));
insert into course values (1, 'math'), (2, 'science'), (3, 'business');

create table student_course (student_id int, course_id int, grade char(1));
insert into student_course values
(1, 1, 'C'), (1, 2, 'C'), (1, 3, 'C'),
(2, 1, 'A'), (2, 2, 'A'), (2, 3, 'A'),
(3, 1, 'A'), (3, 2, 'C'), (3, 3, 'C'),
(4, 1, 'A'), (4, 2, 'C'), (4, 3, 'F');

查询

select s.*, case when all_a_grades.student_id is not null then 'GOOD' else 'MEH' end as grades

from student s 

left join (
    -- find students who got A in all classes
    select student_id, count(distinct ca.id) as aclasses, count(distinct sc.course_id) as allclasses
    from student_course sc
    left join (select id, 'A' as agrade from course) ca 
      on ca.id = sc.course_id and ca.agrade = sc.grade
    group by student_id
    having aclasses = allclasses
) all_a_grades on all_a_grades.student_id = s.id

where not exists (
    -- let's make sure we filter OUT students who have failed
    -- at least one course
    select 1 
    from (
        -- find students who have failed at least one course
        select distinct student_id
        from student_course
        where grade not in ('A', 'B', 'C')
    ) t where t.student_id = s.id
)

结果

| id | fullname | grades | 
| 1  | john     | MEH    | 
| 2  | mary     | GOOD   | 
| 3  | matt     | MEH    |