title: Use the result of the MySQL SELECT query as a WHERE condition in the same query
body: <p>I'm trying to do this query:</p>
  
  <pre><code>SELECT MAX(`peg_num`)
    AS "indicator"
  FROM `list`
  WHERE `list_id` = 1
    AND "indicator" &gt;= 1
  </code></pre>
  
  <p>But I'm getting the result of <code>NULL</code>. What I should be getting is <code>99</code>, as the range of <code>peg_num</code> is 00 to 99.</p>
  
  <p>The value checked against "indicator" should actually be a user input, so I want it to be versatile. But, it <em>does</em> give me the correct result if I flip the equality around:</p>
  
  <pre><code>SELECT MAX(`peg_num`)
    AS "indicator"
  FROM `list`
  WHERE `list_id` = 1
    AND "indicator" &lt;= 1
  </code></pre>
  
  <p>Why would it do this?</p>
  
  <p>Edit:</p>
  
  <p>As suggested, I'm using the HAVING clause... but I just ditched the alias for now anyway:</p>
  
  <pre><code>SELECT MAX(`peg_num`) AS "indicator"
  FROM `list`
  GROUP BY `list_id`
  HAVING MAX(`peg_num`) &lt;= 40
  </code></pre>
  
  <p>Still very stubborn. It gives me 99 now no matter the value in the having clause, regardless of the inequality.</p>
  
  <p>Edit2:</p>
  
  <p>As a clarification:</p>
  
  <p>What I want to happen is the query select the largest value in the range of peg_num, but only if it is larger than a user-given input. So, the max in this case is 99. If the user wants to select a number like 101, he/she can't because it's not in the range.</p>