0

I have 2 tables student and attendance. I need to join both tables and get the result like number of total absents and number of student in particular class and classname. I need a single query to get the below results

tbl_student:

admission_no(PK)          student_name        student_class
345                           John                 X A
352                           Sachin               X A
322                           Steve                IX A
123                           Pinky                X A
343                           Rose                 IX A

tbl_admission:

admission_no(FK)             date_absent
354                          2015-03-30
123                          2015-03-30
322                          2015-03-30

Result should be like this:

 Date_absent        total_absent        total_students      student_class
 2015-03-30            2                    3                  X A
 2015-03-30            1                    2                  IX A
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

1

you can do something like this to manage it in single query:

SELECT date_absent, total_absent, total_students, a1.student_class
FROM (
  SELECT date_absent, count( tbl_admission.admission_no ) AS total_absent, tbl_student.student_class
  FROM `tbl_admission`
  JOIN `tbl_student` ON tbl_student.admission_no = tbl_admission.admission_no
  GROUP BY tbl_student.student_class
)a1
JOIN (
  SELECT count( tbl_student.admission_no ) AS total_students, tbl_student.student_class
  FROM tbl_student
  GROUP BY tbl_student.student_class
)a2
ON a1.student_class = a2.student_class
ORDER BY `a1`.`student_class` DESC 
Ruprit
  • 733
  • 1
  • 6
  • 23
  • Thank you soo much.. You saved my life.....I tried this past 2 days. I was totaly upset.. thanks alot ... keep going .. all the best – Nidhin Sasankan Mar 31 '15 at 10:30