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

Talk about the 8 user instincts behind user experience in design

Editor's note: This article is contributed by...

Installation method of mysql-8.0.17-winx64 under windows 10

1. Download from the official website and unzip h...

Detailed explanation of JavaScript onblur and onfocus events

In HTML pages, visual elements such as buttons an...

HTML form and the use of form internal tags

Copy code The code is as follows: <html> &l...

Use of Linux ipcs command

1. Command Introduction The ipcs command is used ...

Steps to create a Vite project

Table of contents Preface What does yarn create d...

How to use cookies to remember passwords for 7 days on the vue login page

Problem Description In the login page of the proj...

Vue shopping cart case study

Table of contents 1. Shopping cart example 2. Cod...

Javascript implements simple navigation bar

This article shares the specific code of Javascri...

vue-cli introduction and installation

Table of contents 1. Introduction 2. Introduction...

4 ways to avoid duplicate insertion of data in Mysql

The most common way is to set a primary key or un...

Three ways to align div horizontal layout on both sides

This article mainly introduces three methods of i...

How to use display:olck/none to create a menu bar

The effect of completing a menu bar through displ...

SQL Practice Exercise: Online Mall Database User Information Data Operation

Online shopping mall database-user information da...