
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we update the values in one MySQL table by using the values of another MySQL table?
For updating the values in one MySQL table by using the values from another MySQL table, we need to use sub-query as an expression in the SET clause of the UPDATE statement.
Example
In this example, we have two table ‘student’ and ‘info’. We will update the value of column ‘grade’ of table ‘student’ by using the values from column ‘remarks’ of ‘info’ table.
mysql> select * from student; +----+---------+-------+ | Id | Name | grade | +----+---------+-------+ | 1 | Rahul | NULL | | 2 | Gaurav | NULL | | 3 | Raman | NULL | | 4 | Harshit | NULL | | 5 | Aarav | NULL | +----+---------+-------+ 5 rows in set (0.01 sec) mysql> select * from info; +------+-----------+ | id | remarks | +------+-----------+ | 1 | Good | | 2 | Good | | 3 | Excellent | | 4 | Average | | 5 | Best | +------+-----------+ 5 rows in set (0.00 sec) mysql> UPDATE STUDENT SET grade = (SELECT remarks from info WHERE info.id = student.id) WHERE id > 0; Query OK, 5 rows affected (0.08 sec) Rows matched: 5 Changed: 5 Warnings: 0
The query above, with the help of sub-query, updates the values in grade column of ‘student’ table. It can be observed from the result set returned by following MySQL query.
mysql> Select * from student; +----+---------+-----------+ | Id | Name | grade | +----+---------+-----------+ | 1 | Rahul | Good | | 2 | Gaurav | Good | | 3 | Raman | Excellent | | 4 | Harshit | Average | | 5 | Aarav | Best | +----+---------+-----------+ 5 rows in set (0.00 sec)
- Related Articles
- How can we update values in a MySQL table?
- How can we update any value in MySQL view as we can update the values in MySQL table?
- How can we update MySQL table after padding a string with the values of the column?
- How can we find the duplicate values available in a MySQL table by using JOINS?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- How can you update certain values in a table in MySQL using Python?
- How can we update MySQL table after removing a particular string from the values of column?
- How can we add values into the columns of a MySQL table?
- How can we handle NULL values stored in a MySQL table by using PHP script?
- How to update a MySQL table by swapping two column values?
- How do I select data from one table only where column values from that table match the column values of another table in MySQL?
- Updating a MySQL table with values from another table?
- Update data in one table from data in another table in MySQL?
- How can we subtract values in MySQL table with the help of LEFT JOIN?
- How can we combine the values of two or more columns of MySQL table?

Advertisements