title: conditional set in MySQL
body: <p>I have a database of funny images, but this question relates to everything with a posting date. If I am updating a page I'm first checking if there's an entry set up for today, if not I take one that has been in the database the longest, like this:</p>
  
  <pre><code>$result=mysql_query("SELECT * FROM amazingjokes.img WHERE listed=CURDATE()");
  if(!mysql_num_rows($result)){
     $result=mysql_query("SELECT * FROM amazingjokes.img WHERE isnull(listed) 
                          ORDER BY  added ASC");
     $row=mysql_fetch_array($result);
     $mysql_query("UPDATE amazingjokes.img SET listed=CURDATE() 
                   WHERE id=$row[id]");
  }
  </code></pre>
  
  <p>Basically what this does is "get today's image and select the longest pending one when there's no image for today". Is it possible to do this in a single query and how would that be done? </p>