相同的結果我試圖將數據插入到兩個表,其中表2沒有主鍵,但只有外國的:選擇最後一個ID返回多個請求
insert into table1 ....;
insert into table2 (a, column2, c) values(1, (select id from table1 order by inserted_at desc limit 1), 33);
insert into table1 ....;
insert into table2 (a, column2, c) values(1, (select id from table1 order by inserted_at desc limit 1), 33);
insert into table1 ....;
insert into table2 (a, column2, c) values(1, (select id from table1 order by inserted_at desc limit 1), 33);
和我結束了同一個ID中所有3個請求中的table2
爲column2
。爲什麼以及我該如何修復?
你確定你在所有3次迭代中都不在'table1'中插入類似的數據?很難理解,除非你提供了'table1'的精確插入語句,並解釋瞭如何使用'inserted_at'。我猜你在插入sysdate,但沒有插入語句,我們只能猜測。 – Utsav
使用串行列。不要自己計算ID - 數據庫會爲你做這件事。 –
@戈登林諾夫,我沒有。 – Johshi