title: mysql get or create object id function, need to do double select, why?
body: <p>I want to write function that will check if object exists in database - return its id
  or if not - create object and return its id.</p>
  
  <p>So i create this code:</p>
  
  <pre><code>drop function if exists getAuthorId;
  
  DELIMITER  //
  
  CREATE FUNCTION getAuthorId(anonym varchar(255) CHARACTER SET utf8, ispublic tinyint(1))     RETURNS INT
     BEGIN
     DECLARE y INT;
     IF (SELECT id INTO y FROM library_author where library_author.slug = getSlug(anonym) limit 1) THEN
  
      **# Here is need to make second select, cause if I just return y - it will be empty 
      Why ??**
  
         SELECT id INTO y FROM library_author where library_author.slug = getSlug(anonym) limit 1;
     RETURN y;
  ELSE
      INSERT INTO author set author.anonym = anonym, author.slug = getSlug(anonym), author.is_public = ispublic, author.date_created = CURDATE();
      RETURN last_insert_id();
  END IF;
  END // 
  
  DELIMITER ;
  </code></pre>
  
  <p>Is it any method to use only one select request ?</p>