I have two tables class_tbl (id,class_name) and student_tbl(id,class_id,student_name).
I want to display all class in a table format and want to show number of students in each class.
How to do this in one mysql query?
I have two tables class_tbl (id,class_name) and student_tbl(id,class_id,student_name).
I want to display all class in a table format and want to show number of students in each class.
How to do this in one mysql query?
Use this:
SELECT class_name, count(student_tbl.id) as number_of_students FROM class_tbl c INNER JOIN student_tbl s ON c.id=s.class_id GROUP BY class_id
;
You have provided very little information in your question. Please take some time to read some of the question on stackoverflow which are closed / answered / up-voted and spend some time thinking about why they have merited such action.
Assuming that your "student_tbl" actually decomposes the N:M relationship between students and classes (otherwise, as Honeyboy Wilson says, it is impossible to resolve the query) all you need is a count of records in the table grouped by class_id. If you want to add the name of the class then its just a join. This is remedial level SQL and already covered in other answers.