I tried to read about the lock in MySQL, but the topic still remains completely incomprehensible.
The essence of the problem:
Table with the following columns: id - Index, Primary, Autoincrement; id_order - Order Id; name - the name of the ordering person
The bottom line is that id increases without my intervention, but I have to prescribe the next id_order myself.
Procedure:
- Take the last
id_order - We perform calculations in PHP.
- Add to the table a row with
id_order, equal to either the last + 1 or 0.
The bottom line is that with 2 simultaneous orders, you get 2 new entries with the same id_order .
As I understand it, SELECT ... FOR UPDATE will not help me here, as it will unlock the last record immediately after it receives the order_id.
Ie, in theory, I need to lock the record with the last id_order , perform all the actions I need, and then unlock it.
I can't figure out how to do this.
Please no suggestions for changing the table structure. Yes, crookedly done initially.