假设有一张这样的表:
表名:student
字段:ID(学号), sex(性别), class_id(班级)想求出每个班级中sex = '男'的数目
用一个SQL语句,怎么弄?
求大侠们指点

解决方案 »

  1.   


    select class_id,count(1) from student
    where sex='男'
    group by class_id
      

  2.   


    select class_id,count(distinct id) as cnt
    from student
    where sex = '男'
    group by class_id
      

  3.   

    但你的需求不要遍历,只要
    select class_id,count(*) from student where sex='男' group by class_id就行了.
      

  4.   

    SELECT CLASS_ID,COUNT(1) AS '数目'
    FROM STUDENT 
    WHERE SEX = '男'
    GROUP BY CLASS_ID
      

  5.   

    select
      class_id,sum(case sex when '男' then 1 else 0 end) as 数目
    from
      student
    gorup by
      class_id
      

  6.   

    游标,identity加循环,都可以吧
      

  7.   

    不用遍历,直接Group By 就行了
      

  8.   

    select class_id , count(1) 数目 from student where sex = '男' group by class_id