select t1.id,t1.name,t2.finishedBy,t2.project,t2.id as taskID, if($status='','', t1.status) as projectstatus from TABLE_PROJECT as t1 left join TABLE_TASK as t2 on t1.id=t2.project where t1.deleted='0' and t2.deleted='0' and t2.finishedBy!='' and if($beginDate='',1,t1.begin>=$beginDate) and if($endDate='',1,t1.end<=$endDate) having projectstatus=$status