房天下 > 房天下问答 > 业主生活 > 其他
  • sql 统计人数

    SQL查询,在student表中有字段名为subject,name,stu_id,birthday,address,class,memo,请查询所有subject为英语,政治,数学,计算机,C语言编程的学生并统计出人数!

    提问者:swatert

    发布于2010-10-29

共1个回答
  • 茶的氤氲 丨Lv 4
    select count(stu_id) from student where subject in{‘英语’,‘政治’,‘数学’,‘计算机’,‘C语言编程'}上述SQL语句为查询科目为这五门课的学生总数,如果用count(*),可能没有剔除重复记录,所以用count(stu_id)select subject, count(stu_id) from student where subject in{‘英语’,‘政治’,‘数学’,‘计算机’,‘C语言编程'} group by subject分别查询上述五门科目,每门科目的学生总数,返回的是这样的数据对(pair):(英语,50) (政治, 45)……select distinct name from student where subject in{‘英语’,‘政治’,‘数学’,‘计算机’,‘C语言编程'}查询选择上述五门课的所有学生名字,必须加上关键词distinct,以除去重复的名字(比如同一个学生可以同时选上述五门课)select subject, distinct name from student where subject in {‘英语’,‘政治’,‘数学’,‘计算机’,‘C语言编程'}group by subject分别查询上述五门科目各科的学生名字,返回结果为(科目,学该科目的学生名字)
    +1 2010-10-30 举报
热门人气推荐
免责声明:问答内容均来源于互联网用户,房天下对其内容不负责任,如有版权或其他问题可以联系房天下进行删除。