现在在做一个自考系统,要求统计报考和缺考的人数,缺考人的成绩约定为-1.
scoreinfo(stu_id varchar(30),course_id varchar(30),exam_score varchar(10))
其中scoreinfo为表名,stu_id为学生的编号,在这里为外键.course_id为课程编号,在表中
也是外键.exam_score为考试成绩,当缺考时,其值为-1.并且一个人可以考同一门课程多次,
第一次考同一门课程以后的的次数都算作补考。现在要统计报考人数和缺考人数,在网上看到如下语句的解决方法,但是自己试了很多此都报这个语句语法错误,
请高手知道的帮个忙!不胜感激!!!
select stu_id,course_id,count(stu_id),sum((if exam_score='-1',10) for all) from scoreinfo
group by stu_id,course_id.

解决方案 »

  1.   

    报考人数
    select count(distinct stu_id) from scoreinfo;
    缺考人数
    select count(distinct stu_id) from scoreinfo where exam_score='-1';
      

  2.   

    我需要通过一条sql语句查出来!通过两条sql语句我早就知道了
      

  3.   

    MYSQL:
    select stu_id,course_id,count(stu_id),sum(if(exam_score='-1',10,0))
    from scoreinfo
    group by stu_id,course_id.
      

  4.   

    select stu_id,course_id,count(stu_id),sum(if(exam_score='-1',1,0)) 
    from scoreinfo 
    group by stu_id,course_id;注意上述语句是人次,不是人数。如果是要人数的话,按照 1楼 的就可以了。不过会有一种重复,即这个人参加了2门,缺考了1门,则两连都会计数。
      

  5.   


    不知道那sum(if(exam_score='-1',10,0))中10的意思。不管首考还是补考,
    每次考试都算作是报考的。所以报考人数只要计算scoreinfo表中的stu_id的数目即可。
      

  6.   

    一个人可以考同一门课程多次的情况,对于每个考生每一门课程的报考人数和缺考人数为:SELECT stu_id, course_id, count(stu_id), sum(if(exam_score='-1',1,0)) 
    FROM scoreinfo 
    GROUP BY stu_id, course_id;??为什么把exam_score定义为字符类型,而不是整数类型??