Mysql conditional data insertion

Source: Internet
Author: User

The requirements are as follows: I have a table with the basic data, and a field is the management number. My goal is to first query the latest management number, generate a new management number according to the rules, and then insert the new data into the table because the query does not lock the table. so the two threads may find the same management number in the middle of the insert query? The key is the following MYSQL statement that inserts multiple records: [SQL] INSERT INTO clients (client_id, client_name, client_type) SELECT supplier_id, supplier_name, 'advertising' FROM suppliers WHERE not exists (select * from clients where clients. client_id = suppliers. supplier_id); INSERT a single record: [SQL] INSERT INTO clients (client_id, client_name, client_type) SELECT 10345, 'ibm ', 'advertising' FROM dual WHERE not exists (select * from clients wh Ere clients. client_id = 10345); therefore, you can check that the management number does not exist during insertion. If the insertion is unsuccessful, the latest management number is retrieved and re-calculated before insertion until the operation is successful. There are many such cases, such as tickets for trains and cinemas. Due to the large amount of concurrent processing, tickets with tickets are often found. However, this ticket has been sold when an order is placed, I don't know how these systems are handled. Author meditatorzhang

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.