How to query and update the same table in MySQL database at the same time

How to query and update the same table in MySQL database at the same time

In ordinary projects, I often encounter this problem: I need to update and query data in one bid at the same time. For example, if there is a table of data as shown below, the update operation now needs to be: update the name value of status=1 to the name value of id=2

這里寫圖片描述

Usually we think of the following statement to achieve this requirement:

UPDATE tb_testSET NAME = ( SELECT NAME FROM tb_test WHERE id= 2)WHERE `status` = 1

However You can't specify target table 'tb_test' for update in FROM clause . You can't operate on the same table. Change your thinking. If it's not the same table, it should be possible. Therefore, you can treat the selected result as a temporary intermediate table and obtain the desired update-related data from the intermediate table. Therefore, the above update statement can be changed to the following:

UPDATE tb_testSET NAME = (select name from ( SELECT NAME FROM tb_test WHERE id = 2) as t)WHERE `status` = 1

This completes the operation in the question. The general process is: query the data of id=2 as the intermediate table t; query the set data from the t table; perform the update operation so that you do not update and select the same table in the same statement, because this is equivalent to operating two tables, tb_test and the intermediate table t. The final result is as follows:

這里寫圖片描述

You may also be interested in:
  • Summary of Mysql update multi-table joint update method
  • An example of how to query data in MySQL and update it to another table based on conditions
  • How to use a field in one table to update a field in another table in MySQL
  • Mysql updates certain fields of another table based on data from one table (sql statement)
  • How to update another table in mysql
  • A solution to update the increase/decrease range and increase/decrease rate of the entire table with only one SQL statement

<<:  Encapsulate a simplest ErrorBoundary component to handle react exceptions

>>:  How to count the number of specific characters in a file in Linux

Recommend

The main idea of ​​​​dynamically setting routing permissions in Vue

I have seen some dynamic routing settings on the ...

Vue.js implements the nine-grid image display module

I used Vue.js to make a nine-grid image display m...

Detailed explanation of CentOS configuration of Nginx official Yum source

I have been using the CentOS purchased by Alibaba...

Detailed steps to install python3.7 on CentOS6.5

1. Download Python 3 wget https://www.python.org/...

How to write configuration files and use MyBatis simply

How to write configuration files and use MyBatis ...

Solution - BASH: /HOME/JAVA/JDK1.8.0_221/BIN/JAVA: Insufficient permissions

1) Enter the folder path where the jdk file is st...

A brief discussion on the three major issues of JS: asynchrony and single thread

Table of contents Single thread asynchronous Sing...

2017 latest version of windows installation mysql tutorial

1. First, download the latest version of MySQL fr...

NodeJS realizes image text segmentation

This article shares the specific code of NodeJS t...

Summary of @ usage in CSS (with examples and explanations)

An at-rule is a declaration that provides instruc...