MySQL select results to perform update example tutorial

MySQL select results to perform update example tutorial

1. Single table query -> update

UPDATE table_name
SET field1=new-value1, field2=new-value2
[WHERE Clause]

2. Multi-table joint query -> update

UPDATE a
INNER JOIN (SELECT yy FROM b) c ON a.id = c.id 
SET a.xx = c.yy
[WHERE Clause]
  • The above INNER JOIN can be replaced by LEFT JOIN, RIGHT JOIN and other joint queries.
  • The field after SET must be a field in table a. The field can be equal to a constant or a column. If it is not a field in table a, the message "The target table b of the UPDATE is not updatable" will be reported.
  • The WHERE clause must be placed after SET
  • The a table after UPDATE is not the result of a query, nor can it be a SELECT clause.

example:

tableex_copy1 table

tablein_copy1 table

Query SQL

SELECT * FROM
tableex_copy1 a LEFT JOIN
(SELECT * FROM tablein_copy1) b
ON a.BID = b.AID
WHERE b.ASEX = 'female'

update SQL

UPDATE
#SELECT * FROM
tableex_copy1 a LEFT JOIN
(SELECT * FROM tablein_copy1) b
ON a.BID = b.AID
SET a.CESHI = '6666'
WHERE b.ASEX = 'female'

Tableex_copy1 table after update

Summarize

This is the end of this article about MySQL executing update on select results. For more relevant content about MySQL executing update on select results, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A brief discussion on MySQL select optimization solution
  • Solve the problem that MySQL read-write separation causes data not to be selected after insert
  • 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

<<:  HTML table tag tutorial (17): table title vertical alignment attribute VALIGN

>>:  Front-end state management (Part 2)

Recommend

Detailed explanation of the text-fill-color property in CSS3

What does text-fill-color mean? Just from the lit...

HTML+CSS to achieve surround reflection loading effect

This article mainly introduces the implementation...

Install Percona Server+MySQL on CentOS 7

1. Environmental Description (1) CentOS-7-x86_64,...

Introduction to vim plugin installation under Linux system

Table of contents Install vim plugin manager Add ...

Example of automatic stop effect after text scrolling

The effect is very simple, just copy the following...

Detailed explanation of the process of using docker to build minio and java sdk

Table of contents 1minio is simple 2 Docker build...

W3C Tutorial (8): W3C XML Schema Activities

XML Schema is an XML-based alternative to DTD. XM...

dl, dt, dd list label examples

The dd and dt tags are used for lists. We usually...

MySQL 5.7.25 installation and configuration method graphic tutorial

There are two types of MySQL installation files, ...

React's method of realizing secondary linkage

This article shares the specific code of React to...

How to monitor Linux server status

We deal with Linux servers every day, especially ...

Vue implements the method of displaying percentage of echart pie chart legend

This article mainly introduces the pie chart data...

Teach you how to implement the observer mode in Javascript

Table of contents What is the Observer Pattern? S...