Access the auto-increment value when executing an INSERT INTO statement

I am currently using MySQL. I have a table with the id field auto_increment and the imgname field containing a row that is the name of the image file.

I need to generate the "imgname" value using the auto_increment value that is generated using the INSERT INTO statement. The problem is that I do not know this value until I can use mysql_insert_id AFTER the insert request has been started. I would like to know if it is possible to access this value DURING an insert request anyway, and then use it to generate my string in the request.

Thanks in advance.

0
source share
2 answers

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

+1
source

Take a look at LAST_INSERT_ID() . If performance is not an issue, INSERT regularly, then UPDATE with LAST_INSERT_ID() , for example:

 UPDATE table SET name = CONCAT(name, "-", LAST_INSERT_ID()) WHERE id = LAST_INSERT_ID(); 
+1
source

Source: https://habr.com/ru/post/1444898/


All Articles