MYSQL prevents duplicate sample code and mysql sample code when adding a shopping cart

Source: Internet
Author: User
Tags mysql insert

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.