Solve the problem that MySQL read-write separation causes data not to be selected after insert

Solve the problem that MySQL read-write separation causes data not to be selected after insert

MySQL sets up independent writing separation. If you write the following in the code, problems may occur

//Enter first this.insert(obj); 
// Query again Object res = this.selectById(obj.getId());
res: null;

A pitfall online: after doing read-write separation, there is a scenario where you want to reuse the method and only pass in an ID. You can directly find an object in the database for subsequent processing. As a result, you can't find it. Various transaction isolation levels have been checked. Finally, it is found that it is a problem of read-write separation, so change the idea to implement it.

Additional knowledge: MySQL INSERT insertion condition judgment: insert if it does not exist

We often need to perform batch inserts in SQL, requiring: if the record does not exist, insert it; if it does exist, do not insert it. How about using an INSERT statement?

For ordinary INSERT insertion, if we want to ensure that no duplicate records are inserted, we can only create a unique constraint for a certain field;

Is there a solution that does not create a unique constraint and can be achieved with just one INSERT INTO statement?

Answer: Use INSERT INTO IF EXISTS. The specific syntax is as follows

INSERT INTO table(field1, field2, fieldn) 
SELECT 'field1', 'field2', 'fieldn' 
FROM DUAL
WHERE NOT EXISTS(SELECT field FROM table WHERE field = ?)

example:

INSERT INTO a (order_id, operator, oper_date, memo) 
SELECT '3', 'onion3', '2017-11-28', 'Test 3' 
from DUAL 
where not exists(select order_id from a where operator='onion3' and memo = '测试3');

The above article on solving the problem of not being able to select data after insert due to MySQL read-write separation is all the content that the editor shares with you. I hope it can give you a reference, and I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • A brief discussion on MySQL select optimization solution
  • MySQL select results to perform update example tutorial
  • How MySQL Select Statement is Executed
  • Detailed example of using the distinct method in MySQL
  • Should I use distinct or group by to remove duplicates in MySQL?
  • The difference between distinct and group by in MySQL
  • Let's talk about the LIMIT statement in MySQL in detail
  • MySQL series tutorial on understanding the use of union (all) and limit and exists keywords
  • The impact of limit on query performance in MySQL
  • Use of select, distinct, and limit in MySQL

<<:  Implementing a simple whack-a-mole game in JavaScript

>>:  After docker run, the status is always Exited

Recommend

Detailed explanation of Nginx reverse proxy example

1. Reverse proxy example 1 1. Achieve the effect ...

js canvas realizes rounded corners picture

This article shares the specific code of js canva...

HTML multi-header table code

1. Multi-header table code Copy code The code is a...

Delete the image operation of none in docker images

Since I usually use the docker build command to g...

How to set the style of ordered and unordered list items in CSS

In an unordered list ul>li, the symbol of an u...

Detailed explanation of Js class construction and inheritance cases

The definition and inheritance of classes in JS a...

CSS3 realizes the glowing border effect

Operation effect: html <!-- This element is no...

Detailed explanation of three methods of JS interception string

JS provides three methods for intercepting string...

Detailed explanation of three relationship examples of MySQL foreign keys

This article uses examples to describe the three ...

MYSQL custom function to determine whether it is a positive integer example code

You can write a function: Mainly use regular expr...

How to delete a MySQL table

It is very easy to delete a table in MySQL, but y...

How to turn local variables into global variables in JavaScript

First we need to know the self-calling of the fun...

How to quickly insert 10 million records into MySQL

I heard that there is an interview question: How ...

A detailed introduction to the CSS naming specification BEM from QQtabBar

BEM from QQtabBar First of all, what does BEM mea...

Docker's flexible implementation of building a PHP environment

Use Docker to build a flexible online PHP environ...