Short Version: How can I get the maximum value of a column and use the result when adding a row with INSERT INTO statement?
Long Version: I have read the question: How to use 'select ' in MySQL 'insert' statement and tried to follow the instructions for its answers. This is my query:
INSERT INTO employee (id, name, state, supervisorId) SELECT MAX(id)+1, 'Dan', state, supervisorId FROM employee WHERE name='Chris';
But I get this error:
ERROR: 1062: Duplicate entry '7' for key 'PRIMARY'
The reason is that I have another row whose id is 7. Consequently,
MAX(id)
doesn't return the actual maximum value, but a value equal to id of the row containing 'Chris' which is 6.
What am I doing wrong?