×

MySQL UPDATE JOIN

The UPDATE JOIN is a MySQL statement used to perform cross-table updates that means we can update one table using another table with the JOIN clause condition. The "update join" query modifies and operates join columns. This query can work on single or multiple tables and always requires the SET and WHERE clauses. The SET operator uses the regular column or row to modify the table value. The "WHERE" clause applies the join condition on the table. Sometimes, the update join works on several tables using primary key and foreign key.

Syntax

The syntax of the update inner join shows below.

UPDATE table1, table2 INNER JOIN table1 
ON table1.column1 = table2.column1
SET table1.column2 = table2.column2, table2.column2 = expression
WHERE join_condition;

The syntax of the update left join shows below.

UPDATE table1, table2 LEFT JOIN [RIGHT JOIN] table1 
ON table1.column1 = table2.column1
SET table1.column2 = table2.column2, table2.column2 = expression
WHERE join_condition;

Basic settings of the update join

  • Create the first table department with a category of education columns.
mysql> CREATE TABLE department (
    	department_id INT AUTO_INCREMENT,
    	department_name VARCHAR(45),
    	admissions INT,
    	PRIMARY KEY (department_id));
  • Insert data into the department table. Here, the table includes the department name and number of the admissions.
mysql> INSERT INTO department (department_name, admissions) VALUES("Bachelor of Arts", 45), ("Bachelor of Commerce", 55), ("Bachelor of Engineering", 50), ("Bachelor of Technology", 60);
  • Create a second table subject with the required columns.
mysql> CREATE TABLE subject (
	subject_id INT AUTO_INCREMENT,
    	subject_name VARCHAR(45),
    	students INT,
   	PRIMARY KEY (subject_id));
  • Insert data into the subject table. Here, the table includes the subject name and number of the students.
mysql> INSERT INTO subject (subject_name, students) VALUES("Electronics", 20), ("Electrical", 25), ("Computer Science", 15), ("Information Technology", 30);
  • You can see the output of the department table structure and its data using the below query.
mysql> SELECT * FROM department;
UPDATE JOIN

The above image shows the information of the table, such as department id, department name, and the number of the admission.

  • You can see the output of the subject table structure and its data using the below query.
mysql> SELECT * FROM subject; 
UPDATE JOIN
  • Add the foreign key to the department table. The subject_id column is a common column that links department and subject table.
UPDATE JOIN

Examples of the MySQL update join

1) Example: update the inner join with the "WHERE" clause example shows below.

Execute the below query to set the admissions column whose value increases by 1. You need to use the "ON" and SET keywords to change specific columns. The "UPDATE" keyword requires for modifying the given table.

mysql> UPDATE department d INNER JOIN subject s ON d.subject_id = s.subject_id SET admissions = admissions + 1 WHERE department_id < 2;

OUTPUT

Execute the below query to get updated table data.

mysql> Select * from department;
UPDATE JOIN

The above image shows that the admission number increases. This column changes from 44 to 45 numbers. Here, the table change data of the first row of the column.

2) Example

The following query will set the admissions column in the table whose value decreases by 1. The "ON" and SET keywords need to change specific columns. The "UPDATE" keyword requires for modifying the given table. The "WHERE" clause updates the specific row.

mysql> UPDATE department d INNER JOIN subject s ON d.subject_id = s.subject_id SET admissions = admissions - 2 WHERE department_id < 2;

OUTPUT

Execute the below query to get updated table data.

mysql> Select * from department;
UPDATE JOIN

The above image shows that the admission number is decremented from 45 to 43 numbers. Here, the table changes the data of the first row of the column.

3) Example: update the inner join with the "SET" operator example shows below.

The following query will set the admissions column in the table whose value decreases by 10. The "ON" and SET keywords are used to change specific columns. The UPDATE keyword requires modifying the given table.

mysql> UPDATE department d INNER JOIN subject s ON d.subject_id = s.subject_id SET admissions = admissions – 10;

OUTPUT

Execute the below query to get updated table data.

mysql> Select * from department;
UPDATE JOIN

The above image shows that the admission number is decremented from 43 to 33 numbers. Here, the table data changes the entire rows of the column.

4) Example: update the right join with the "SET" operator example shows below.

The following query will set the admissions column in the table whose value increases by 10. You need to use the "ON" and SET keywords to change specific columns. The UPDATE keyword requires for modifying the given table.

mysql> UPDATE department d RIGHT JOIN subject s ON d.subject_id = s.subject_id SET admissions = admissions * 2;

OUTPUT

Execute the below query to get updated table data.

mysql> Select * from department;
UPDATE JOIN

The above image shows the change in the admission number. This column changes from the original number to double numbers of the origin. Here, the table data changes the entire rows of the column.

5) Example: update the multiple columns using the left join example shows below.

Execute the following query to update the table using left join. The multiple columns changes by using the clause and operator.

mysql> UPDATE department d LEFT JOIN subject s ON d.subject_id = s.subject_id SET department_name = " Bachelor of Agriculture", admissions = admissions / 2 WHERE department_id = 2;

OUTPUT

Execute the below query to get updated table data.

mysql> Select * from department;
UPDATE JOIN

The above image shows the change in the department name and admission number. This column changes string and number. Here, the table data changes the second row of the column.

6) Example: update the right join with the "WHERE" clause example shows below.

Execute the following query to update the table using the right join. The department name changes by using the clause and operator.

mysql> UPDATE department d RIGHT JOIN subject s ON d.subject_id = s.subject_id SET department_name = " Bachelor of Fine Arts" WHERE department_id = 1;

OUTPUT

Execute the below query to get updated table data.

mysql> Select * from department;
UPDATE JOIN

The above image shows the change of the department name. This column changes from the "bachelor of arts" number to the "bachelor of fine arts". Here, the table data changes the first row of the column.


Related Topics

MySQL POW() function

In this context, we will learn how we can use the MySQL POW() function with proper syntax and good examples. Introduction of MySQL POW() function This function in MySQL is used to...

2 minutes read.

MySQL ISNULL function

This function is used to check the null value in table data. It returns the result in a Boolean form. If table data is null, then the output becomes 1....

2 minutes read.

MySQL vs SQL

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL Descending Index

Descending Index It is a type of index that stores key values in descending order. This query improves the performance of an index query. MySQL system displays the index as per...

3 minutes read.

MySQL FIND_IN_SET() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function MySQL provides a built-in string function called...

4 minutes read.

MySQL EQUI JOIN

An equijoin is an operation that combines multiple tables based on equality or matching column values in the associated tables. This operation links more than two tables based on a...

5 minutes read.

MySQL SEC_TO_TIME() function

In this context, we will learn how we can use the MySQL SEC_TO_TIME() function with proper syntax and good examples. Introduction of MySQL SEC_TO_TIME() function This function in MySQL is used to...

2 minutes read.

MySQL Unique index

It helps to maintain data integrity to enforce the uniqueness of values in one or more columns. We can create more than one UNIQUE index in a single table, which...

3 minutes read.

MySQL Data Types

Introduction of the Data types In the data file, we have several information such as names, numbers, marks, and other information. This information specifies categories such as numbers, characters, and decimal...

8 minutes read.

MySQL Date and Time function

The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the...

8 minutes read.

MySQL Queries

MySQL Queries MySQL supports SQL queries in the MySQL interface. These queries help to interact data with the application. MySQL uses create database, user database, create a table, truncate table, and...

13 minutes read.

MySQL DATEDIFF() function

In this context, we will learn how we can use the MySQL DATEDIFF() function with proper syntax and good examples. Introduction of MySQL DATEDIFF() function DATEDIFF() function in MySQL is used to...

3 minutes read.

MySQL CONCAT() function

In this context, we will learn how we can use the MySQL CONCAT() function with proper syntax and good examples. Introduction of MySQL CONCAT() function By using the MySQL CONCAT function, we...

3 minutes read.

MySQL TAN() Function

Basically, the TAN() function in MySQL is used to give back the tangent of a mentioned number.In any kind of right-angle triangle, the tangent of an angle is the length...

2 minutes read.

MySQL LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.

MySQL Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

13 minutes read.

MySQL MIN function

The MIN() function determines the minimum or lowest value of the data set. This function works on numerical data type values. If the table displays zero value, then the row...

4 minutes read.

MySQL RADIANS() function

In this context, we will learn how we can use the MySQL RADIANS() function with proper syntax and good examples. Introduction of MySQL RADIANS() function RADIANS() function in MySQL is used to...

2 minutes read.

MySQL vs Oracle

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.