I have an SQL table with an auto-increment field. I know people have asked how to retrieve the ID of the ‘last entry’ after the insert has taken place, and I know how to do that. What I want to do is duplicate the auto-generated field in to a second column of the same table – how do I modify my insert code to do this:
mysql_query("INSERT INTO `tags` (`tid`,`tagid`,`tagname`) VALUES('','INSERTED.ID','{$tagname}')");
The reason I want to do this is because I want to be able to make two rows equal each other. I.E. if two different tags (with different unique ids) have the same tagid, then I can use the ‘tagid’ reference elsewhere as a unique identifier and cycle through them. I can also retain knowledge of which tag is the parent tag – because it will have the same ‘tid’ as ‘tagid’.
I have a system for tagging events; but sometimes I miss-spell a tag… so rather than correcting all the mistakes, I want to make my system error proof by allowing miss-spelt tags to be joined up and treated the same way as a the correct tag.
Would appreciate your help – even if that means doing this a completely different way.
Thanks,
2 options:
1) You can use
LAST_INSERT_ID()mysql function, but in a second query, not in the same.2) Define a insert Trigger in the table, then when you insert, the trigger “triggers” and do the action in the trigger.
Note: Short creator, see more in the link.