INSERT and return ID, or if DUPLICATE KEY return existing ID in MySQL -
i have "tags" table, has unique on "tags" column, prevent duplicates.
i have "bookmarks_tags" table, relates bookmarks tags, , has unique on "bookmark_id" , "tag_id" columns, prevent duplicates.
however, need people able add same tag, , accomplish this, need means of retrieving id of existing tag use reference in "bookmarks_tags" table.
is there way of writing insert if detects duplicate, returns id of duplicate? or, alternatively, insert ... select more appropriate "bookmarks_tags" table?
the key thing here has work under both conditions; add new, or retrieve old.
also, last_insert_id() useless in scenario, tag in question have been added @ time.
any ideas?
one way using insert ignore
:
insert ignore tags (tags,...) values (the_new_tags, ...); select tag_id tags tags=the_new_tags;
Comments
Post a Comment