开发者

Access Auto-Increment Value During INSERT INTO Statement

开发者 https://www.devze.com 2023-03-25 06:24 出处:网络
I am currently using MySQL. I have a table that has an auto_increment \'id\' field, and an \'imgname\' field containing a string that is the file name of an image.

I am currently using MySQL. I have a table that has an auto_increment 'id' field, and an 'imgname' field containing a string that is the file name of an image.

I n开发者_StackOverfloweed to generate the 'imgname' value using the auto_increment value that is create by an INSERT INTO statement. The problem is, I don't know this value until I can use mysql_insert_id, AFTER the insert query has run. I would like to know if it's possible to access this value DURING the insert query somehow and then use it to generate my string in the query statement.

Thanks in advance.


I would keep the id and imgname independent of each other and combine the two on SELECT when needed. If the need is frequent enough, create a view.


Have a look at LAST_INSERT_ID() function. If performance is not an issue, INSERT regularly, and then UPDATE using LAST_INSERT_ID(), like:

UPDATE table SET name = CONCAT(name, "-", LAST_INSERT_ID()) WHERE id = LAST_INSERT_ID();
0

精彩评论

暂无评论...
验证码 换一张
取 消