I need two id columns in the same table that create unique values upon insert. Since MySQL allows only one column to auto-increment, I need to do something like max(id)+1 for the other column.
Here’s the SQL I expected to work:
INSERT INTO invoices (invoiceid)
VALUES ((SELECT MAX(invoiceid)+1 FROM invoices))
The select statement works independently, but within my INSERT, it’s not allowed. I get the error : You can’t specify target table ‘invoices’ for update in FROM clause
You want to use
INSERT INTO .... SELECT FROMinstead ofINSERT INTO...VALUES():My question for you would be why are you not use an
AUTO INCREMENTfield to generate theinvoiceidvalue? That is what it is for, then you will not have to create this when inserting data.