title: Potentially complicated query for hasMany relationship; can this be done?
body: <p>A <code>Person</code> can have many <code>Degree</code> records. </p>
  
  <p>Person</p>
  
  <pre><code>+-----------+--------------+------+-----+---------+----------------+
  | Field     | Type         | Null | Key | Default | Extra          |
  +-----------+--------------+------+-----+---------+----------------+
  | id        | int(11)      | NO   | PRI | NULL    | auto_increment |
  | firstName | varchar(60)  | NO   |     | NULL    |                |
  | lastName  | varchar(100) | NO   |     | NULL    |                |
  +-----------+--------------+------+-----+---------+----------------+
  </code></pre>
  
  <p>Degree <em>(edit: I've hidden many additional fields for simplicity)</em></p>
  
  <pre><code>+----------+-------------------------------+------+-----+---------+----------------+
  | Field    | Type                          | Null | Key | Default | Extra          |
  +----------+-------------------------------+------+-----+---------+----------------+
  | id       | int(11)                       | NO   | PRI | NULL    | auto_increment |
  | personID | int(11)                       | NO   | MUL | NULL    |                |
  | type     | enum('PhD','MSE','MEng','MA') | NO   |     | NULL    |                |
  +----------+-------------------------------+------+-----+---------+----------------+
  </code></pre>
  
  <p>On a web page, I want to display a list of people and their degree information <em>(there's other info to a degree other than their type, I've just not included it for simplicity)</em>. I believe this will have to be done in N+1 queries. First, a query to select all people that have degrees:</p>
  
  <pre><code>SELECT DISTINCT person.id
  FROM person
  JOIN degree ON degree.personID = person.id
  </code></pre>
  
  <p>And then when iterating through each result, query for their degrees directly:</p>
  
  <pre><code>SELECT * FROM degree WHERE personID = :personID
  </code></pre>
  
  <p>However, my life is not that simple. I have two additional requirements:</p>
  
  <ol>
  <li>Allow filtering the page based on degree. ex: "Only show people with 'MA' degrees", "Only show people with 'MSE' degrees".</li>
  <li>Only show 'MA' degrees if the person does not also have a 'PhD' degree.</li>
  </ol>
  
  <p>I'm struggling with how to incorporate these requirements into the queries. For the first requirement, the main query needs to know beforehand what degrees the user has. Do I need a sub-query here?</p>
  
  <p>The rule also somehow needs to be involved in the main query (as well as second query that loads the degrees).</p>