Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's because in your <code>WHERE</code> condition you're wanting <code>subj='sub1'</code> AND <code>subj='sub2'</code> simultaneously, which can never be possible.</p> <p>One way to do it is to JOIN the table with <code>sub</code> and <code>mark</code> (<code>enrolees</code>?). This is because the student has to pass <em>BOTH</em> sub1 and sub2 (as opposed to passing AT LEAST ONE, in which case you can use <code>and subj IN ('sub1','sub2')</code>:</p> <pre><code>SELECT e1.student, sdb.lastname, sdb.firstname FROM students_db sdb JOIN enrollees e1 ON e1.student=sdb.student_id JOIN enrollees e2 ON e2.student=sdb.student_id WHERE e1.sub = 'sub1' AND e1.remark = 'Pass' AND e2.sub = 'sub2' AND e2.remark = 'Pass' </code></pre> <p>This ensures that the student passes <em>both</em> sub1 and sub2.</p> <p>However, it's a bit sucky - if you have 5 prerequisites, all necessary, you have to <code>JOIN</code> in <code>enrollees</code> 5 times.</p> <p>One way you can get around this, is to SUM up the number of passes they got. Then you don't have to JOIN:</p> <pre><code>SELECT e.student, sdb.lastname, sdb.firstname, FROM students_db sdb JOIN enrollees e ON e.student=sdb.student_id WHERE e.remark='Pass' AND e.sub IN ('sub1','sub2') -- list of subjects here GROUP BY e.student HAVING SUM(IF(e.remark='Pass',1,0))=2 -- number of subjects in the list here </code></pre> <p>This counts up how many of the <strong>required subjects</strong> (in this case, 'sub1' and 'sub2') the student has passed, and makes sure that is equal to 2 (since they have to have passed all subjects).</p> <p>This has the advantage of you being able to put in <em>as many prerequisite subjects as you want</em> (provided all are required prerequisites) -- you just have to list them in the <code>WHERE</code> and change the total in the <code>HAVING</code>. The number of joins does not grow with the number of prerequisites.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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