Question: I am trying to insert a new row and set the customer\_id with max()+1. The reason for this is the table already has a auto\_increatment on another column named id and the table will have multiple rows with the same customer\_id.

With this:

```
INSERT INTO customers
  ( customer_id, firstname, surname )
VALUES 
  ((SELECT MAX( customer_id ) FROM customers) +1, 'jim', 'sock')

```

...I keep getting the following error:

```
#1093 - You can't specify target table 'customers' for update in FROM clause

```

Also how would I stop 2 different customers being added at the same time and not having the same customer\_id?

Answer: 
None of the about answers works for my case. I got the answer from [here](https://stackoverflow.com/questions/4429319/you-cant-specify-target-table-for-update-in-from-clause), and my SQL is:

```
INSERT INTO product (id, catalog_id, status_id, name, measure_unit_id, description, create_time)
VALUES (
  (SELECT id FROM (SELECT COALESCE(MAX(id),0)+1 AS id FROM product) AS temp),
  (SELECT id FROM product_catalog WHERE name="AppSys1"),
  (SELECT id FROM product_status WHERE name ="active"),
  "prod_name_x",
  (SELECT id FROM measure_unit WHERE name ="unit"),
  "prod_description_y",
  UNIX_TIMESTAMP(NOW())
)

```
You can't do it in a single query, but you could do it within a transaction. Do the initial MAX() select and lock the table, then do the insert. The transaction ensures that nothing will interrupt the two queries, and the lock ensures that nothing else can try doing the same thing elsewhere at the same time.