title: Complex MySql Query in PHP
body: <p>I have these tables:</p>
  
  <ul>
  <li>exams (medical exams)</li>
  <li>users</li>
  <li>institutions (medical institutions)</li>
  <li>users_institutions (join table)</li>
  <li>log (here i'm keeping date and time of Login, Logout, Downloads Files)</li>
  </ul>
  
  <p>Users belongs to one or more institution, exams belong only to one institution. So, users can search into exam table only for exams that belong to their institution. The query that i use to get this is the following:</p>
  
  <pre><code>SELECT * FROM exams, institutions, users_institutions 
  WHERE institutions.institution_pk = users_institutions.institution_fk
  AND exams.institution_fk = institution.institution_pk
  AND users.users_pk = {$user_id}
  </code></pre>
  
  <p>This code works fine. My problem is this:</p>
  
  <p><strong>When a user download a PDF file from an exam I save date, time, user ID, exam ID, log type (1 for download, 2 for Login and 3 for Logout). I need a SQL Query to do a search of all exams that have not been downloaded yet and belong to their institutions for a particular user.
  This means that if a have 10 users in a institution I need that every user have their own result set.</strong></p>
  
  <p>Thanks!</p>
  
  <p>(Sorry for my poor english)</p>