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.`date`>=$startDate and t1.`date`<=$endDate having dept=$dept order by `date` asc