Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple JOINS - Mysql
    primarykey
    data
    text
    <p>I have 4 tables:</p> <p>Classes:</p> <pre><code>id class 1 X C 2 VI B 3 XII F 4 X C 5 VI B 6 VIII A </code></pre> <p>Students:</p> <pre><code> id id_student class 2 1 [-&gt;] X C 3 4 [-&gt;] VI B 5 7 [-&gt;] X C </code></pre> <p>Grades</p> <pre><code>Id id_student grade 3 1 [-&gt;] 8 4 1 [-&gt;] 7 5 1 [-&gt;] 10 6 1 [-&gt;] 8 7 1 [-&gt;] 9 8 4 [-&gt;] 5 9 4 [-&gt;] 7 10 4 [-&gt;] 9 11 1 [-&gt;] 4 12 1 [-&gt;] 9 13 7 [-&gt;] 10 </code></pre> <p>Absences:</p> <pre><code>id id_student 1 1 [-&gt;] 2 1 [-&gt;] 3 1 [-&gt;] 4 4 [-&gt;] 5 1 [-&gt;] 6 1 [-&gt;] 7 1 [-&gt;] 8 7 [-&gt;] </code></pre> <p>What I want is to select all classes from Classes Table (only those who have students) and count the number of students for every class, the average of their grades and number of absences.</p> <p>This is what I have:</p> <pre><code>SELECT classes.class, COUNT(distinct students.id_students) AS nr_students, AVG(grades.grade) AS avg_grades FROM classes INNER JOIN students ON students.student = clases.class INNER JOIN grades ON students.students = grades.id_student GROUP BY class </code></pre> <p>Until now everything is great. Output (PHP):</p> <pre><code>Class Nr_Students Avg_Grades VI B 1 7.0000 X C 2 8.1250 </code></pre> <p>But I want to count every row from Absences table and print the total number of absences for every class.</p> <p>This is what I'm trying to do:</p> <pre><code>Class Nr_Students Avg_Grades Nr_Abcences VI B 1 7.0000 1 X C 2 8.1250 7 </code></pre> <p>I can't change the DB!</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload