查找按主题名称注册多个主题的学生

时间:2015-01-16 06:59:35

标签: mysql sql select join

StudentID | SubCode | SubName
-------------------------------
   1          1       Math
   1          2       Science
   1          3       English
   2          1       Math
   2          2       Science
   3          2       Science
   4          1       Math
   4          3       English

这是我的主题表。

如何找到已注册的学生

  1. 仅使用数学注册的学生
  2. 已注册数学和英语的学生
  3. 已注册科学与数学和英语的学生
  4. 在单个SQL查询中。

    我试过这种方式

    SELECT DISTINCT  
        `stud_id` FROM  `subj_assign` 
    WHERE  
       `subj_id` =  '1,2'
       AND STATUS =  '1'
    ORDER BY  
       `subj_assign`.`stud_id` ASC     
    

5 个答案:

答案 0 :(得分:2)

尝试这两个查询,两者都相似,但以不同的方式显示数据:

SELECT StudentID,
       CASE
         WHEN Sum(CASE
                    WHEN SubCode IN( 1, 2, 3 ) THEN 1
                    ELSE 0
                  END) = 3 THEN 'All'
         WHEN Sum(CASE
                    WHEN SubCode IN( 1, 3 ) THEN 1
                    ELSE 0
                  END) = 2 THEN 'MathsEnglish'
         WHEN Sum(CASE
                    WHEN SubCode IN( 1 ) THEN 1
                    ELSE 0
                  END) = 1 THEN 'Maths'
       END AS subjects
FROM   yourtable
GROUP  BY StudentID
HAVING subjects IS NOT NULL;

SELECT StudentID,
       CASE
         WHEN Sum(CASE
                    WHEN SubCode IN( 1, 2, 3 ) THEN 1
                    ELSE 0
                  END) = 3 THEN 'YES'
         ELSE 'NO'
       END AS `all`,
       CASE
         WHEN Sum(CASE
                    WHEN SubCode IN( 1, 3 ) THEN 1
                    ELSE 0
                  END) = 2 THEN 'YES'
         ELSE 'NO'
       END AS `MathsEnglish`,
       CASE
         WHEN Sum(CASE
                    WHEN SubCode IN( 1 ) THEN 1
                    ELSE 0
                  END) = 1 THEN 'YES'
         ELSE 'NO'
       END AS `Maths`
FROM   yourtable
GROUP  BY StudentID 

SQLFIDDLE

答案 1 :(得分:1)

试试这个:

SELECT s.stud_id, 
      (CASE WHEN A.stud_id IS NOT NULL THEN 'Yes' ELSE 'No' END) OnlyMaths, 
      (CASE WHEN B.stud_id IS NOT NULL THEN 'Yes' ELSE 'No' END) MatchsAndEnglish, 
      (CASE WHEN C.stud_id IS NOT NULL THEN 'Yes' ELSE 'No' END) MatchsAndEnglishANDScience
FROM students s
LEFT JOIN ( SELECT stud_id 
            FROM subj_assign 
            WHERE subj_id = 1 AND STATUS =  '1'
          ) AS A ON s.stud_id = A.stud_id
LEFT JOIN ( SELECT stud_id 
            FROM subj_assign 
            WHERE subj_id IN (1, 3) AND STATUS =  '1'
            GROUP BY StudentID 
            HAVING COUNT(DISTINCT subj_id) = 2
          ) AS B ON s.stud_id = B.stud_id
LEFT JOIN ( SELECT stud_id 
            FROM subj_assign 
            WHERE subj_id IN (1, 2, 3) AND STATUS =  '1'
            GROUP BY StudentID 
            HAVING COUNT(DISTINCT subj_id) = 3
          ) AS C ON s.stud_id = C.stud_id

答案 2 :(得分:1)

您需要使用Having Clause过滤群组。我不知道为什么你需要在一个查询中获得所有结果。试试这个。

SELECT StudentID,
       'Only Maths' as Subjects
FROM   #testt
GROUP  BY StudentID
HAVING Count(CASE WHEN SubCode = '1' THEN 1 END) = 1
       AND Count(*) = 1 
UNION ALL
SELECT StudentID,
       'Maths and English'
FROM   #testt
GROUP  BY StudentID
HAVING Count(CASE WHEN SubCode = '1' THEN 1 END) = 1
       AND Count(CASE WHEN SubCode = '3' THEN 1 END) = 1
UNION ALL
SELECT StudentID,
       'Maths,Sceince and English'
FROM   #testt
GROUP  BY StudentID
HAVING Count(CASE WHEN SubCode = '1' THEN 1 END) = 1
       AND Count(CASE WHEN SubCode = '3' THEN 1 END) = 1
       AND Count(CASE WHEN SubCode = '2' THEN 1 END) = 1 

答案 3 :(得分:0)

您可以使用union来爬升多个查询返回的记录。

select * from table where subName like 'Math'
union 
select * from table where subName like 'Math' and subName like 'English'
union 
select * from table where subName like 'Math' and subName like 'English' and subName like 'Science'

答案 4 :(得分:0)

如果您已经显示了完整的表格,那么只有三个主题,并且提供的学生ID和主题ID对于表格是唯一的,只需对主题ID求和:

select stud_id, group_concat(sub_name)
from subj_assign
group by stud_id
having sum(subj_id) = 1 -- Math only
or sum(subj_id) = 4 -- Math and English only
or sum(subj_id) = 6 -- Math and English and Science
order by stud_id asc;

如果表包含更多主题:

select stud_id, group_concat(sub_name)
from subj_assign
group by stud_id
having sum(subj_id) = 1 -- Math only
or (min(subj_id) = 1 and max(subj_id) = 3 and sum(subj_id) = 4) -- Math and English only
or (min(subj_id) = 1 and max(subj_id) = 3 and sum(subj_id) = 6) -- Math and English and Science
order by stud_id asc;

当然,您可以使用许多其他聚合进行评估,例如count(*)count(case when ... end)max(case when ... end)