Summary of Mysql update multi-table joint update method

Summary of Mysql update multi-table joint update method

Next, I will create two tables and execute a series of SQL statements. By carefully observing the changes in the data in the tables after the SQL statements are executed, it is easy to understand the usage of multi-table joint update.

student table class table

1. Execute UPDATE student s , class c SET s.class_name='test00',c.stu_name='test00' WHERE s.class_id = c.id

student table class table

2. Execute UPDATE student s JOIN class c ON s.class_id = c.id SET s.class_name='test11',c.stu_name='test11'

student table class table

3. Execute UPDATE student s LEFT JOIN class c ON s.class_id = c.id SET s.class_name='test22',c.stu_name='test22'

student table class table

4. Execute UPDATE student s RIGHT JOIN class c ON s.class_id = c.id SET s.class_name='test33',c.stu_name='test33'

student table class table

5. Execute UPDATE student s JOIN class c ON s.class_id = c.id SET s.class_name=c.name , c.stu_name=s.name

student table class table

Additional knowledge points:

MySQL multi-table association update

In daily development, we usually write single-table update statements, and rarely write updates that associate multiple tables.

Unlike SQL Server, in MySQL, there are some minor differences in the methods used for multi-table join updates and multi-table join queries for select.

Let’s look at a specific example.

update orders
  left join users u
    on o.userId = u.id
set o.userName = u.name;

In the above example, the update keyword is followed by a result set of a multi-table association. MySQL directly regards this multi-table association result set as a single table, and then performs regular update operations based on this single table.

The slight difference from SQL Server is that the set clause of SQL Server follows the specific table to be updated, while the set statement of MySQL follows the result set to be updated (the specific table to be updated is set in the set statement).

Summarize

This concludes this article on the summary of MySQL update multi-table joint update methods. For more relevant MySQL update multi-table joint update content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL batch update and batch update of different values ​​of multiple records
  • Several ways to update batches in MySQL
  • Batch modification, clearing, copying and other update commands for MySQL data table field contents
  • MySQL implements batch update of data in different tables

<<:  Extract specific file paths in folders based on Linux commands

>>:  Solution to the problem of returning 0x1 when the Windows 2008 task plan fails to execute a bat script

Recommend

SQL implementation of LeetCode (178. Score ranking)

[LeetCode] 178.Rank Scores Write a SQL query to r...

Three ways to draw a heart shape with CSS

Below, we introduce three ways to draw heart shap...

FlashFXP ftp client software registration cracking method

The download address of FlashFXP is: https://www....

Use of TypeScript Generics

Table of contents 1. Easy to use 2. Using generic...

Detailed explanation of JavaScript array deduplication

Table of contents 1. Array deduplication 2. Dedup...

Commands to find domain IP address in Linux terminal (five methods)

This tutorial explains how to verify the IP addre...

A brief discussion on Linux virtual memory

Table of contents origin Virtual Memory Paging an...

Detailed analysis of compiling and installing vsFTP 3.0.3

Vulnerability Details VSFTP is a set of FTP serve...

An example of how to optimize a project after the Vue project is completed

Table of contents 1. Specify different packaging ...

Summary of learning Docker commands in one article

Table of contents Introduction Mirror repository ...

MySQL cursor detailed introduction

Table of contents 1. What is a cursor? 2. How to ...

How does JS understand data URLs?

Table of contents Overview Getting started with d...

js to achieve simple product screening function

This article example shares the specific code of ...

How to visualize sketched charts in Vue.js using RoughViz

introduce A chart is a graphical representation o...