MYSQL prevents duplicate sample code and mysql sample code when adding a shopping cart
Preface
Recently, due to work reasons, I placed an order to pay for this item in the APP shopping cart. A bug was raised in the test. When the point is added to the shopping cart, two items appear in the same item.
This is because when you add the product to the shopping cart, the first step is to determine whether the product to be added to the shopping cart is already in the shopping cart. If so, add one based on the original quantity. if no plug-in is available.
Because the two steps are not atomic operations, there is a multi-thread security problem. I will not talk much about them below. Let's take a look at the detailed solution process with xiaobian, it will be helpful to you.
MySQL insert has a relatively advanced operation.
Sample Code:
Insert into t_xs_shopping_cart (user_id, shop_id, commodity_id, quantity) VALUES (71, 67,140 201057403511024, 1) on duplicate key update quantity = quantity + 1
When the unique key constraint takes effect, the update statement is used to increase the number by 1.
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.