title: Mysql random unique number not working
body: <p>I'm trying to get a unique code number which doesn't exists in the "codes" table using a query I found in several other SO threads:</p>
  
  <p><a href="https://stackoverflow.com/a/25435263/471573">https://stackoverflow.com/a/25435263/471573</a>
  <a href="https://stackoverflow.com/a/11680058/471573">https://stackoverflow.com/a/11680058/471573</a></p>
  
  <p>I'm currently trying to use this code:</p>
  
  <pre><code>CREATE TABLE codes (
    code int NOT NULL
  );
  
  INSERT INTO `codes` (`code`) VALUES 
  (1),(2),(3),(4),(5); 
  
   SELECT FLOOR(RAND() * 10) AS `rand_code` 
     FROM `codes` 
    WHERE "rand_code" NOT IN (SELECT `code` FROM `codes`) 
    LIMIT 1
  </code></pre>
  
  <p>sqlfiddle:</p>
  
  <p><a href="http://sqlfiddle.com/#!9/5e807/2" rel="nofollow noreferrer">http://sqlfiddle.com/#!9/5e807/2</a></p>
  
  <p>With lots of issues:</p>
  
  <ol>
  <li>if no codes are in the table, I get no results at all.</li>
  <li>if there ARE any codes, the results is not unique. I can get a result already in the table...</li>
  </ol>
  
  <p>Any idea on how to make this work?</p>
  
  <p>Thanks!</p>