现在知道部门和员工表,员工表的department和部门ID关联现在想统计没个部门有多少员工,因为部门表有个员工人数字段peopleamount,我想把统计出来的人数对应部门存入peopleamount字段

解决方案 »

  1.   

    UPDATE department t
    SET peopleamount = (SELECT COUNT(*) FROM employee e WHERE e.department = t.id)
      

  2.   

    create table department_temp
    (部门ID,
    peopleamount,
    cnt 
    )
    as 
    select 部门ID,peopleamount,count(*) from department group by 部门ID,peopleamountupdate department set peopleamount = (select cnt from department_temp where 部门ID = department.部门ID)
      

  3.   

    update 部门表 set 
    部门表.peopleamount = (select count(rowid) from 员工表 where 员工表.department=  部门表.部门ID)
      

  4.   

    HelloWorld_001(Hello) ( ) 信誉:100    Blog   加为好友  
    說的有可能啊,
      

  5.   

    楼主出现了没办法比1楼的sql还优化了
      

  6.   

    应该在两表department 和id字段上建立索引
      

  7.   

    oracle就是这个比较方便
    update还能用子句