在mysql中分组时从status获取学生结果

w6lpcovy  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(488)

我已经从下面的记录中创建了一个临时表。我想用学生id(stu\d)对学生分组。分组时,如果他通过了所有科目,则学生成绩状态为通过;如果他至少一个科目不通过,则学生成绩状态为不通过。提前谢谢

h6my8fg2

h6my8fg21#

尝试此查询

SELECT stu_Name,result
    FROM `marks`
    WHERE stu_D not in
    (SELECT stu_D FROM marks WHERE  result='FAILED')
    GROUP BY stu_D
zvms9eto

zvms9eto2#

SELECT 
   T.stu_D, T.stuName, IF(T.number_of_failures>0, 'FAILED', 'PASSED') final_result
FROM
(SELECT 
   stu_D, stuName, COUNT(IF(result='FAILED', 1, NULL)) number_of_failures
 FROM your_table
 GROUP BY stu_D, stuName) T;
ipakzgxi

ipakzgxi3#

你可以试试下面的查询-

SELECT stu_D
   ,stuName
   ,CASE 
    WHEN T2.CNT = 0 
         THEN 'passed' 
         ELSE 'failed' 
    END status 
FROM (SELECT stu_D
            ,stuName
            ,COUNT(CASE
                   WHEN result = 'FAILED' 
                       THEN 1
                   END) CNT
     FROM T
     GROUP BY stu_D
             ,stuname) T2

相关问题