lnguyen55
lnguyen55

Reputation: 737

What is wrong with this INSERT MAX+1 query?

I'm having problem with this query:

INSERT INTO invoices(invuid, linenumber) VALUES (?, SELECT(SELECT max(linenumber)+1 FROM invoices WHERE invuid=?))

It keeps saying: "General error: 1093 You can't specify target table 'invoices' for update"

Upvotes: 1

Views: 24

Answers (1)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79929

Your syntax is invalid, use INSERT INTO ... SELECT and remove the values part like this:

INSERT INTO invoices(invuid, linenumber) 
SELECT ?, max(linenumber)+1 FROM invoices WHERE invuid=?

Upvotes: 1

Related Questions