select er.vstdate,ifnull(h.day_name,date_format(er.vstdate,'%W')) 'dayname'
,count(er.vn) cc
from er_regist er
left join ovst o on er.vn=o.vn
left join holiday h on h.holiday_date=er.vstdate
where er.vstdate between '2012-1-1' and '2012-1-31'
and (dayofweek(er.vstdate) in (1,7)
or o.visit_type='O'
or h.holiday_date is not null)
group by er.vstdate