部门表 部门ID           deparet_id
部门名称         name
部门人员中间表  部门人员中间表ID  pd_id
部门表ID          deparet_id
人员表ID          police_id人员表人员表ID         police_id
姓名              name人员请假表人员请假表ID      pq_id
人员表ID          police_id
部门表ID          deparet_id
需求:
每个部门有多少人,有多少人请假.不管写存储过程,还是SQL句语都可以谢谢

解决方案 »

  1.   

    select deparet_id, count(deparet_id) 
    from 部门人员中间表 
    group by deparet_id;select deparet_id, count(deparet_id) 
    from 人员请假表 
    group by deparet_id;
      

  2.   

    select name,count(case when exists(
      select 1 from 人员请假表  where deparet_id=t.deparet_id) then 1 end)"请假人数",
    count(1)"总人数"
    from 部门表 t
    group by deparet_id,name
      

  3.   

    ..搞错了,改下
    select a.name,count(case when exists(
      select 1 from 人员请假表 where police_id=b.police_id) then 1 end)"请假人数",
    count(1)"总人数"
    from 部门表 a,部门人员中间表 b
    where a.deparet_id=b.deparet_id
    group by a.deparet_id,a.name
      

  4.   

    select mt.deparet_id, mt.name
    (select count(st1.deparet_id) from 部门人员中间表 st1 where st1.deparet_id = mt.deparet_id) as "每个部门有多少人",
    (select count(st2.deparet_id) from 人员请假表 st2 where st2.deparet_id = mt.deparet_id) as "每个部门有多少人请假"
    from 部门表 mt;
      

  5.   

    浪哥果然厉害,不过还是劝你不要把太多的逻辑写在一个sql里,这样可能会增加其符合。