数据表为 tbl
有3个列,列名分别为A B C,
A为CHAR
B为CHAR
C为INT 
A的值就只有 m 和 f ,分别代表男和女
B的值是班级,比如3-1 (三年级一班)
C的值是班级人数数据库举个例子是这样的A  B   C
m  3-1 34 
f  3-1 29
m  3-2 33
f  3-2 30 

3年级1班,男生34人,女生29人
3年级2班,男生33人,女生30人现在我想统计下所有班级当中,男生的人数,我该如何写命令查询呀?本人对命令不熟,谢谢。

解决方案 »

  1.   

    select sum(c) from tbl where A='m';
      

  2.   

    非常感谢 ACMAIN,再问一个问题,
    假设我要列出 哪几个班级的男生人数是一样的,我该如何表达。再次感谢。
      

  3.   

    是总人数还是每个班的人数?
    总人数一楼的就可以了
    select  c from tb1 where A='m';
    这是每个班的人数
      

  4.   

    select * from tb1 where c in(select c from tb1 group by c having count(1)>1);
      

  5.   

    少了个条件select * from tbl where a='m' and c in(select c from tbl group by c having count(1)>1);
      

  6.   

    create table tb1
    (
      a char(1),
      b char(3),
      c int
    );
    insert tb1 select
    'm', '3-1', 34 union select
    'f', '3-1', 29 union select
    'm', '3-2', 33 union select
    'f', '3-2', 30 union select
    'm', '3-3', 33;
    create table tbss
    (
      b char(3),
      count_boy int
    );insert tbss
    select b,sum(c) as count_boy
    from tb1
    where a='m'
    group by b;select s.b
    from tbss s join(
    SELECT count_boy
    from tbss
    group by count_boy
    having count(*)>1) z on s.count_boy=z.count_boy
    /*
    3-2
    3-3
    */