×

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 RAND() function

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

3 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 MONTHNAME() function

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

3 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 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.

MySQL Constraints

MySQL Constraints Introduction The constraints help to restrict what values should be stored in a table. The constraints provide limitations of the columns or data. This function helps to insert data in...

20 minutes read.

MySQL ELT() function

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

2 minutes read.

MySQL Environmental Setup

The MySQL Environmental Setup MySQL is free, open-source, and cross-platform software, which can be downloaded from its official website. MySQL management system installs on Linux, macOS, and windows. MySQL requires a framework...

6 minutes read.

MySQL Clauses

MySQL Clauses MySQL system clauses are keywords or statements to handle information. It helps to operate a group of the data and apply it to require conditions. The clauses apply conditions...

16 minutes read.

MySQL CEIL() function

In this context, we will learn how we can use the MySQL CEIL () function with proper syntax and good examples. Introduction of MySQL CEIL() function Input is taken by the CEIL()...

3 minutes read.

MySQL Advance table Query

MySQL Advance table Query The table is created by index, rows, and columns in the MySQL database. The user saves their information in matrix format. The database requires a query to...

14 minutes read.

MySQL LOG() function

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

3 minutes read.

MySQL MOD() 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 Basically, in MySQL, the MOD() function is...

3 minutes read.

MySQL REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 minutes read.

MySQL LOG10() function

In this context, we will learn how we can use the MySQL LOG10() function with proper syntax and good examples. Introduction of MySQL LOG10() function To evaluate the natural logarithmic value of...

2 minutes read.

MySQL COALESCE function

This statement displays the first non-null value of the table data. The COALESCE function returns a null value when all values of the table are null or do not find...

3 minutes read.

MySQL Triggers

MySQL trigger is a function of the stored procedure to respond to the system program. This function responds and runs any data table event automatically. You can use it for...

5 minutes read.

MySQL LEAST function

This statement displays the smallest value of the table. The LEAST function returns a null value when the table contains a null value. If the table contains all numerical values,...

2 minutes read.

MySQL PI() function

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

2 minutes read.

MySQL Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.