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

A simple example of using Vue3 routing VueRouter4

routing vue-router4 keeps most of the API unchang...

Detailed explanation of the use of nohup /dev/null 2>&1

nohup command: If you are running a process and y...

Implementing a table scrolling carousel effect through CSS animation

An application of CSS animation, with the same co...

Why is IE6 used by the most people?

First and foremost, I am a web designer. To be mor...

Innodb system table space maintenance method

Environmental Description: There is a running MyS...

Several ways to generate unique IDs in JavaScript

Possible solutions 1. Math.random generates rando...

How to operate Docker and images

Find mirror We can search for images from the Doc...

js realizes horizontal and vertical sliders

Recently, when I was doing a practice project, I ...

Scary Halloween Linux Commands

Even though it's not Halloween, it's wort...

JavaScript super detailed implementation of web page carousel

Table of contents Creating HTML Pages Implement t...

Detailed explanation of three ways to set borders in HTML

Three ways to set borders in HTML border-width: 1...

CentOS 6-7 yum installation method of PHP (recommended)

1. Check the currently installed PHP packages yum...

How to add links to FLASH in HTML and make it compatible with all major browsers

Look at the code first Copy code The code is as fo...