MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

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.