选择所有没有成绩不佳的学生

时间:2017-08-31 04:12:19

标签: php mysql

enter image description here

我的查询有问题。我想选择所有没有成绩不佳的学生。示例

student ID  SECTION GRADE   SUBJECT
--------------------------------------
12345       A       1.5     ITC01
56789       A       2.0     ITC02
12345       A       5.0     ITC02
56789       B       1.5     ITC01 

SELECT COUNT(DISTINCT stud_id) AS student FROM grade WHERE grade BETWEEN 1 AND 3 and stud_id ='$student' AND section='$section'

但是当我查询时,成绩不合格的学生仍然会被添加到没有成绩不及的学生人数中。

我希望有这样的输出

SECTION     NO. OF STUDENT  IN-TRACK    OUT-TRACK 
   A        2               1           1   
   B        1               1           0 
谢谢你们!

3 个答案:

答案 0 :(得分:0)

请尝试以下查询:

SELECT section as SECTION, COUNT(stud_id) AS `NO. OF STUDENT`, 
SUM(if(grade IN(1,5), 1, 0)) `IN -TRACK`, SUM(if(grade < 1, 1, 0)) `OUT-TRACK` 
FROM grades 
GROUP BY stud_id, section

表名,intrack和outtrack条件可以是你想要的。

答案 1 :(得分:0)

在当前的sql语句中,它会将grade列中的记录计为1 and 3之间的记录。

你必须这样做。此查询将为您提供高于或等于3的学生人数。

SELECT COUNT(DISTINCT stud_id) AS student FROM 'yourtablename'  
           WHERE grade >= 3
           AND stud_id ='$student' AND section='$section';

答案 2 :(得分:0)

SELECT DISTINCT x.student_id
  FROM my_table x
  LEFT
  JOIN my_table y
    ON y.student_id = x.student_id
   AND y.grade IN(0,5)
 WHERE y.student_id IS NULL;