title: Doctrine Query calculate MAX value in WHERE clause
body: <p>I have this tables:</p>
  
  <p><em>subscribers</em></p>
  
  <pre><code>id | name  |
  ---+-------+
  1  | Hill  |
  2  | Sam   |
  3  | Joe   |
  </code></pre>
  
  <p>subscribers_years</p>
  
  <pre><code>subscriber_id | year  |
  --------------+-------+
  1             | 2009  |
  1             | 2010  |
  1             | 2011  |
  2             | 2009  |
  2             | 2010  |
  3             | 2010  |
  </code></pre>
  
  <p>I want to take an advanced search using only records that have a maximum as x.
  If x = 2010
  Results query must be: Sam and Joe!</p>
  
  <p>I try this:</p>
  
  <pre><code>$query = $this-&gt;createQuery('su')
              -&gt;select('su.id, su.name)
              -&gt;addWhere(
                  '(SELECT MAX(year) AS maxYear FROM `subscribers_years` WHERE `subscriber_id`=`su.id`) = ?',
                  2010
              );
  </code></pre>
  
  <p>but i have the message error: Message: Couldn't find class <code>subscribers_years</code> </p>
  
  <p>how should I write my query right way?</p>
  
  <hr>
  
  <h2>Solution</h2>
  
  <pre><code>$query-&gt;addWhere('(SELECT MAX(year) AS maxYear FROM SubscriberYear WHERE subscriber_id=su.id) = ?', 2010 );
  </code></pre>
  
  <p>my mistake was that I did not use the class name Doctrine as the From, but used the name of the table mysql! :S</p>