各位大侠帮忙啊。我有个sql语句要请教.
我有两张表.一张单位表.film_table.(film_id,film_name) 一张user_table(user_id,user_zaizhi,user_name,user_sex,film_name)
我想查询每个单位的总人数.总的男人,女人数.在职人数

解决方案 »

  1.   


    select 
          film_name ,
          count(1)as '单位总人数',
          (select count(1) from user_table where user_sex='男' and file_name=a.file_name)as '男人',
          (select count(1) from user_table where user_sex='女' and file_name=a.file_name)as '女人',
          (select count(1) from user_table where user_zaizhi='true' and file_name=a.file_name)as'在职数'
    from   user_table a
    group by film_name
      

  2.   

    总人数select count(user_id) from user_table group by film_name总的男人select count(user_sex) from user_table where user_sex='male' group by film_name总的女人select count(user_sex) from user_table where user_sex=female' group by film_name在职人数select count(user_zaizhi) from user_table group by film_name
      

  3.   

    select 
    sum(case when user_zaizhi='在' then 1 else 0 end) 在职人数,
    sum(case when user_sex='女' then 1 else 0 end) 女人,
    sum(case when user_sex='男' then 1 else 0 end) 男人,
    count(*) 总数
    from user_table
    group by film_name
      

  4.   

    hongqi162  ....我帮你该了下就好了..
            select f.film_name,
    sum(case when user_leixing='在职' then 1 else 0 end) 在职人数,
    sum(case when user_sex='女' then 1 else 0 end) 女人,
    sum(case when user_sex='男' then 1 else 0 end) 男人,
    count(*) 总数
    from users_table a,film_table f
    group by f.film_name谢谢你....
      

  5.   

    他直接存的name,还需要改吗?