title: PHP, MySQL: how to select random records without RAND (attempt to avoid RAND())
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/19412/how-to-request-a-random-row-in-sql">How to request a random row in SQL?</a>  </p>
  </blockquote>
  
  
  
  <p>Because of a huge table I cannot use MySQL function <code>RAND()</code> and trying to avoid using <code>RAND()</code> this way:</p>
  
  <pre><code>$a = mysql_fetch_array(mysql_query("SELECT MIN(id), MAX(id) FROM table WHERE category_id='".$var."'"));
  $id = rand($a[0],$a[1]);
  $id2 = rand($a[0],$a[1]);
  </code></pre>
  
  <p>Which doesn't work, because: in <code>$a</code> I have the biggest and smallest ID with the respective <code>category_id</code> - that's ok.</p>
  
  <p>The problem brings the rows with <code>$id</code> and <code>$id2</code> - because there are used the minimal and maximal ID values, but PHP function <code>rand()</code> will take some random value between minimum and maximum - and there is a big probability, that the picked out record will not belongs into <code>category_id</code>.</p>
  
  <p>So I would like to ask you about a way, how could I fix this example.</p>
  
  <p>THank you</p>