My query looks like this:
INSERT INTO table1 (lfd, somedata)
select (select max(lfd) + 1 from table1), somedata from table2
Table2 has multiple rows. So I want multiple rows inserted into table1.
That works, but lfd is set to the same number for all inserted rows.
So if max(ldf) + 1 is “2” for the first row inserted, it’s “2” for all the following rows too. That’s incorrect, because max(lfd) + 1 for the next row should return “3” of course.
How do I tell mysql to reevaluate the subquery for each insert?
I can’t touch the table structures because I migrate data from one php-gallery into a second different php-gallery. The structure of the tables is under the control of the applications.
In Oracle I would simply define a sequence and select the squeencename.nextval in the subselect. – I am pretty sure that would work, but mysql does not offer sequences as far as I know, right?
Try this once:
Essentially this would just use a variable as a sequence, initializing it with the current maximum id from the table.
Note that you might have concurrency issues here if rows are inserted into
table1while this query is running.