update table1 set 分类1=a.分类1,分类2=a.分类2,...分类X=a.分类X from table1 b,( select 员工编号,sum(case when 时间分类='分类1' then 1 else 0 end) as 分类1,sum(case when 时间分类='分类2' then 1 else 0 end) as 分类2, ... sum(case when 时间分类='分类X' then 1 else 0 end) as 分类X from table2 group by 员工编号 ) as a where a.员工编号=b.员工编号 注意:你的table1、table2搞反了。 对两个表的员工编号加索引。
先 select 员工编号,sum(case when 时间分类='分类1' then 1 else 0 end) as 分类1,sum(case when 时间分类='分类2' then 1 else 0 end) as 分类2, ... sum(case when 时间分类='分类X' then 1 else 0 end) as 分类X from table2 group by 员工编号 看是不是你需要的数据再update
员工编号
记录编号
时间分类table2:
记录编号
员工编号
分类1
分类2
……你说的联系是指什么?
set 分类1=a.分类1,分类2=a.分类2,...分类X=a.分类X
from table1 b,(
select 员工编号,sum(case when 时间分类='分类1' then 1 else 0 end) as 分类1,sum(case when 时间分类='分类2' then 1 else 0 end) as 分类2,
...
sum(case when 时间分类='分类X' then 1 else 0 end) as 分类X
from table2 group by 员工编号
) as a
where a.员工编号=b.员工编号
注意:你的table1、table2搞反了。
对两个表的员工编号加索引。
select 员工编号,sum(case when 时间分类='分类1' then 1 else 0 end) as 分类1,sum(case when 时间分类='分类2' then 1 else 0 end) as 分类2,
...
sum(case when 时间分类='分类X' then 1 else 0 end) as 分类X
from table2 group by 员工编号
看是不是你需要的数据再update
以后请多多指教!