I have a "tags" table, which has a UNIQUE on the "tags" column, to prevent duplicates.
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.
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.
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?
The key thing here is that it has to work under both conditions; add as new, or retrieve old.
Also, LAST_INSERT_ID() is useless in this scenario, as the tag in question could have been added at any time.
Any ideas?