JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Use of UPDATE JOIN in MySQL

Author:JIYIK Last Updated:2025/04/11 Views:

This tutorial will explain how to use the statement in MySQL database UPDATE JOIN.

We generally use joinsto iterate over the rows in a particular table which may or may not have similar rows in other tables. We can UPDATEuse JOINthe clause along with the statement to implement multiple table updates.

The basic syntax of MySQL UPDATE JOINis as follows.

UPDATE Table_1, Table_2,
[INNER JOIN] Table_1 ON Table_1.Column_1 = Table_2. Column_1
SET Table_1.Column_2 = Table_2.Column_2, 
    Table_2.Column_3 = expression
WHERE condition

For a specific table in MySQL, the above syntax works as follows.

  • We start our work by specifying table_1the main table named and the table we want to join with the main table as . The tables mentioned in the clause are updated, and the data in the tables not mentioned after the clause is not changed.table_2UPDATEUPDATE
  • Once we do that, we have to mention the type of join we wish to use. In the above syntax, we have used INNERa join. This join must come immediately UPDATEafter the with clause.
  • After providing values ​​for the columns we wish to update, we mention an WHEREWITH clause to specify the specific conditions for the update.

There is another way to use this method as shown below.

UPDATE Table_1, Table_2
SET Table_1.column_2 = Table_2.column_2,
      Table_2.column_3 = expr
WHERE Table_1.column_1 = Table_2.column_1 AND condition

Before we begin, let's create two tables to work with. We'll call these tables student_detailsand marks. These tables can be created using the following code.

CREATE TABLE marks (
    performance INT(11) NOT NULL,
    percentage FLOAT NOT NULL,
    PRIMARY KEY (performance)
);

CREATE TABLE student_details (
    stu_id INT(11) NOT NULL AUTO_INCREMENT,
    stu_name VARCHAR(255) NOT NULL,
    performance INT(11) DEFAULT NULL,
    total FLOAT DEFAULT NULL,
    PRIMARY KEY (emp_id),
    CONSTRAINT fk_performance FOREIGN KEY (performance)
        REFERENCES marks (performance)
);

INSERT INTO marks(performance,percentage)
VALUES(1,0),
      (2,0.01),
      (3,0.03),
      (4,0.05),
      (5,0.08);

INSERT INTO student_details(stu_name,performance,total)      
VALUES('Preet Sanghavi', 1, 50000),
      ('Joe Sand', 3, 65000),
      ('Su Greens', 4, 75000),
      ('Gray Dellop', 5, 125000),
      ('Neon Jonty', 3, 85000),
      ('Peter Foe', 2, 45000),
      ('Little Wayne', 3, 55000);

To visualize student_detailsthe table, we use the following code.

SELECT * FROM student_details;

The above code will give the following output.

stu_id   stu_name 		stu_performance total
1		Preet Sanghavi		1			50000
2		Joe Sand			3			65000
3		Su Greens			4			75000
4		Gray Dellop			5			125000
5		Neon Jonty			3			85000
6		Peter Foe			2			45000
7		Little Wayne		3			55000

Similarly, we can visualize marksthe table.

SELECT * FROM marks;

The above code will give the following output.

performance    percentage 
1				0
2				0.01
3				0.03
4				0.05
5				0.08

From the above code block we can see that the percentage values ​​are marksin the table and we have to use UPDATE JOINand INNER JOINas our primary join to adjust each student's total student_detailstable based on the values ​​of and marksin the table .percentageperformance

Now let's see UPDATE JOINwhat the statement actually does.


UPDATE JOINUsing statement.in in MySQL

We can perform the above update operation using the following query.

UPDATE student_details
        INNER JOIN
    marks ON student_details.performance = marks.performance 
SET 
    total = total + total * percentage;

Here, student_detailstable is totalthe main table whose values ​​need to be updated.

The output of the above query will give the following result.

stu_id   stu_name 		stu_performance total
1		Preet Sanghavi		1			50000
2		Joe Sand			3			66950
3		Su Greens			4			78750
4		Gray Dellop			5			135000
5		Neon Jonty			3			87550
6		Peter Foe			2			45450
7		Little Wayne		3			56650

As we can see in the above code block, each student's is updated 总分according to his/her status in 分数the table .表现

So, with the help of above mentioned techniques, we can use in MySQL effectively Update Join.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

If ELSE in MySQL

Publish Date:2025/04/11 Views:85 Category:MySQL

In this tutorial, we aim to explore how to use IF ELSE the statement in MySQL. One of the key roles of a data analyst is to gather insights from the data and produce meaningful results. It can be done with the help of several data filtering

DATETIME vs. TIMESTAMP in MySQL

Publish Date:2025/04/11 Views:117 Category:MySQL

DATETIME and TIMESTAMP are two different data types that can be used to store values ​​that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea

Execute multiple joins in one query in MYSQL

Publish Date:2025/04/11 Views:94 Category:MySQL

Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.

Joining 3 tables in MySQL

Publish Date:2025/04/11 Views:187 Category:MySQL

In this tutorial, we will learn how to join three tables in MySQL. Businesses and organizations may have to visualize three tables simultaneously based on certain matching columns common to all three tables. This operation is allowed in MyS

How to use the Row_Number() function in MySQL

Publish Date:2025/04/11 Views:142 Category:MySQL

In this tutorial, we will explain how to use the VALUES function in MySQL ROW_NUMBER() . This is a sorting method that assigns consecutive numbers within a partition starting from 1. It is important to note that no two rows within a partiti

Multiple primary keys in MySQL

Publish Date:2025/04/11 Views:66 Category:MySQL

In this tutorial, our goal is to explore the concept of multiple primary keys for a table in MySQL. Many times, businesses and organizations have to assign certain columns as primary keys. This primary key has multiple uses and reasons to b

Displaying foreign keys in MySQL

Publish Date:2025/04/11 Views:55 Category:MySQL

In this tutorial, we aim to explore how to display foreign keys for tables and columns in MySQL. The type of key that references a primary key, also known as the primary key of another table, is called a foreign key. Understanding the forei

Select first N rows in MySQL

Publish Date:2025/04/11 Views:85 Category:MySQL

Sometimes, you have to select first N rows of MySQL database according to your project requirements. n The value of varies according to the requirement; it can be TOP 1 row or TOP 30 rows. We will learn how to select top N rows using the cl

Copying a Database in MySQL

Publish Date:2025/04/11 Views:105 Category:MySQL

Creating a copy of an existing database is known as the MySQL Clone method. Cloning involves creating a copy of the table structure, constraints, functions, procedures, triggers, and all functionality associated with the table in one go. Th

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial