title: INSERT and return ID, or if DUPLICATE KEY return existing ID in MySQL
body: <p>I have a "tags" table, which has a UNIQUE on the "tags" column, to prevent duplicates.</p>
  
  <p>I have a "bookmarks_tags" table, which relates bookmarks to tags, and it has a UNIQUE on the "bookmark_id" and "tag_id" columns, to prevent duplicates.</p>
  
  <p>However, I need people to be able to add the same tag, and to accomplish this, I need some means of retrieving the ID of the existing tag to use as reference in the "bookmarks_tags" table.</p>
  
  <p>Is there a way of writing an INSERT so that if it detects a duplicate, it returns the ID of that duplicate? Or, alternatively, would an INSERT ... SELECT be more appropriate for "bookmarks_tags" table?</p>
  
  <p>The key thing here is that it has to work under both conditions; add as new, or retrieve old.</p>
  
  <p>Also, LAST_INSERT_ID() is useless in this scenario, as the tag in question could have been added at any time.</p>
  
  <p>Any ideas?</p>