1、sum (if())
将表tb_out_people
中的数据根据out_peo_id
、pro_id
、type
进行归并,计算其对应overtime
的和。
SELECT out_peo_id,pro_id,sum(if(type='0',overtime,0 )) as "0",
sum(if(type='1',overtime,0 )) as "1",sum(if(type='2',overtime,0 )) as "2"
FROM tb_out_people
GROUP BY out_peo_id,pro_id
得到的结果为:
2、简单sum()
select out_peo_id,SUM(overtime) as "加班时长"
from temp_test
group by out_peo_id