I am trying to duplicate rows in the same table. Table A has no auto_increment (unfortunately).
INSERT INTO A (id, foreign_id, value)
SELECT (SELECT MAX(id)+1 FROM A), 2, value
FROM A WHERE foreign_id = 1;
But (SELECT MAX(id)+1 FROM A) is calculated once, so it will always return the same ID. At the second row insertion the query will fail (duplicate primary key).
I tried that first:
INSERT INTO A (id, foreign_id, value)
SELECT MAX(id)+1, 2, value
FROM A WHERE foreign_id = 1;
But the MAX(id)+1 takes into account WHERE foreign_id = 1, so there’s no guarantee that I’ll get a non-used ID.
How can I proceed to either:
- Have a subquery that is re-calculated for each
INSERT - Or more generally: duplicate rows in my table given there’s no auto_increment
Have you tried using variables?