title: cakephp complex find query example
body: <p>I would like to perform a <a href="http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Models.html#find-all" rel="nofollow">find('all')</a> query on a table in a CakePHP application with the following conditions:</p>
  
  <p>1- The primary key value is equal 17</p>
  
  <p>AND</p>
  
  <p>2- The value of another column <code>my_column_id</code> in the table is the MAX of all the values of the column <code>my_column_id</code> but less than the value of <code>my_column_id</code> of the datum whose primary key value is equal to 17</p>
  
  <p>AND</p>
  
  <p>3- The value of another column <code>my_column_id</code> in the table is the MIN of all the values of the column <code>my_column_id</code> but greater than the value of <code>my_column_id</code> of the datum whose primary key value is equal to 17</p>
  
  <p>So the above query should give me 3 results.</p>
  
  <p>Is it possible to get all this in one single query? or do I need three separate queries?</p>
  
  <p>I tried something like:</p>
  
  <pre><code>$results = $this-&gt;Model-&gt;find('all', array(
      'conditions' =&gt; array(
          'MAX(Model.my_column_id) &lt;' =&gt; 23,
          'Mddel.id' =&gt; 17,
          'MIN(Model.my_column_id) &gt;' =&gt; 23
      )
  ));
  </code></pre>
  
  <p><code>23</code> represents the value of <code>my_column_id</code> of the datum whose primary key is equal to 17</p>
  
  <p>But that gives me the following error: <code>SQL Error: 1111: Invalid use of group function</code></p>
  
  <p>Any help please.</p>