select t1.account,t1.consumed,t1.`date`,if($dept='0',0,t2.dept) as dept from TABLE_EFFORT as t1 left join TABLE_USER as t2 on t1.account=t2.account where t1.`deleted`='0' and if($startDate='',1,t1.`date`>=$startDate) and if($endDate='',1,t1.`date`<=$endDate) having dept=$dept order by `date` asc