0

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 need 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.

0

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This might be the best idea, but I really would like to do what I asked in my question if it is at all possible.
1

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(); 

1 Comment

Yes, this is what I am currently doing, but I want to do it with one single query if possible.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.