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

Summary of several key points about mysql init_connect

The role of init_connect init_connect is usually ...

Vue Element front-end application development table list display

1. List query interface effect Before introducing...

Solution to ERROR 1054 (42S22) when changing password in MySQL 5.7

I have newly installed MySQL 5.7. When I log in, ...

CSS transparent border background-clip magic

This article mainly introduces the wonderful use ...

Two ways to clear table data in MySQL and their differences

There are two ways to delete data in MySQL: Trunc...

CSS3 uses transform to create a moving 2D clock

Now that we have finished the transform course, l...

MySQL detailed summary of commonly used functions

Table of contents MySQL Common Functions 1. Numer...

The process of installing and configuring nginx in win10

1. Introduction Nginx is a free, open source, hig...

How to update the view synchronously after data changes in Vue

Preface Not long ago, I saw an interesting proble...

Two ways to use react in React html

Basic Use <!DOCTYPE html> <html lang=&qu...

Vue implements verification whether the username is available

This article example shares the specific code of ...

How to manually encapsulate paging components in Vue3.0

This article shares the specific code of the vue3...