This might work for you:select t4.worker_id ,sum(t4.s_time) from (select t3.worker_id ,t3.s_time from (select t1.worker_id ,t1.project_id ,t1.s_time from searchtag1 as t1 where exists (select * from searchtag2 as t2 where t2.worker_id=t1.worker_id ) union all select t2.worker_id ,t2.project_id ,t2.s_time from searchtag2 as t2 where exists (select * from searchtag1 as t1 where t1.worker_id=t2.worker_id ) ) as t3 group by t3.worker_id ,t3.project_id ) as t4 group by t4.worker_id
Maybe we can cut away one subselect, like this:select distinct t3.worker_id ,sum(t3.s_time) from (select t1.worker_id ,t1.project_id ,t1.s_time from searchtag1 as t1 where exists (select * from searchtag2 as t2 where t2.worker_id=t1.worker_id ) union all select t2.worker_id ,t2.project_id ,t2.s_time from searchtag2 as t2 where exists (select * from searchtag1 as t1 where t1.worker_id=t2.worker_id ) ) as t3 group by t3.worker_id ,t3.project_id
ps.: syntax error might occur, as my only developement tool right now is notepad :-D