Even if I could help you with a SQL query (of which I'm
not sure), you would probably still need help integrating the query into your PHP script. However, it looks like you are using some framework which allows you to build database queries in a different way (I'm talking about this fragment:
$attendance = DB::table('wys_teacherattendances') ->where('t_amonth', $amonth) ->where('t_ayear', $ayear) ->get();
– that's not SQL). I'm not familiar with that framework. Overall, it seems to me your question needs to be migrated to Stack Overflow. —
Andriy M 11 secs ago