title: Mysql two tables, select all values from one table, compare if exists and then return One random value
body: <p>I'm new here as for posting questions, but I've been looking for answers since long time ago.
  Still what I'm trying to do now I didn't found for the right solution or I just didn't know how to use it if I did found it.</p>
  
  <p>Here's my SQL query:</p>
  
  <pre><code>$sql = "SELECT * FROM table_links AS link_id WHERE link_points &gt; '1' 
  AND NOT EXISTS (SELECT * FROM table_surf AS slink_id WHERE user_id = '$user'
  AND surf_time = '$time') ORDER by RAND() LIMIT 1";
  </code></pre>
  
  <p>I'm trying to select a random LINK id (wich has points) and check if that link was allready shown to a user today by looking into the 2nd table where all LINK ID's are saved as SLINK ID's with user ID's and time of the last visits.</p>
  
  <p>If last visit was today (<code>$time</code>) then it should not return anything.</p>
  
  <p>I'm  new to PHP/mysql but I'm trying to learn, still this is a situation where I'm not sure if it is even possible to realise a complex query like this one.</p>
  
  <p>UPDATE: Based on Akhil post this is what seems to work for me -</p>
  
  <blockquote>
    <p>$sql = "SELECT * FROM (SELECT RAND() temp, link_id FROM table_links WHERE link_points > '1' AND link_status NOT IN ('spam') AND NOT EXISTS (SELECT slink_id FROM table_surf AS links WHERE user_id = '$user' AND surf_time = '$time' AND slink_id = '$surflink')) a ORDER by temp LIMIT 1";</p>
  </blockquote>