Insert and set value with max()+1 problems

后端 未结 11 1239
难免孤独
难免孤独 2020-12-02 16:43

I am trying to insert a new row and set the customer_id with max()+1. The reason for this is the table already has a auto_increatment on another column named id and the tabl

11条回答
  •  清歌不尽
    2020-12-02 17:36

    You can't do it in a single query, but you could do it within a transaction. Do the initial MAX() select and lock the table, then do the insert. The transaction ensures that nothing will interrupt the two queries, and the lock ensures that nothing else can try doing the same thing elsewhere at the same time.

提交回复
热议问题