
- 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
Update a column based on another MySQL table’s column
For this, you can use the join concept. Let us first create a table −
mysql> create table DemoTable1 -> ( -> Id int, -> Name varchar(10) -> ); Query OK, 0 rows affected (0.51 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(100,'Bob'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1;
This will produce the following output −
+------+------+ | Id | Name | +------+------+ | 100 | Bob | +------+------+ 1 row in set (0.00 sec)
Here is the query to create second table −
mysql> create table DemoTable2 -> ( -> Id int, -> FirstName varchar(10) -> ); Query OK, 0 rows affected (0.49 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2 values(100,'Adam'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable2;
This will produce the following output −
+------+-----------+ | Id | FirstName | +------+-----------+ | 100 | Adam | +------+-----------+ 1 row in set (0.00 sec)
Here is the query to update a column based on another MySQL table’s column −
mysql> update DemoTable1 -> join DemoTable2 on DemoTable1.Id=DemoTable2.Id -> set DemoTable1.Name=DemoTable2.FirstName; Query OK, 1 row affected (0.15 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check the record of first table with updated value −
mysql> select * from DemoTable1;
This will produce the following output −
+------+------+ | Id | Name | +------+------+ | 100 | Adam | +------+------+ 1 row in set (0.00 sec)
- Related Articles
- Update MySQL column based on email address?
- MySQL update a column with an int based on order?
- Exclude rows based on column value when another duplicate column value is found in MySQL?
- MySQL DATE_ADD() to increment a date based on the value in another column?
- Perform MySQL UPDATE on the basis of DATE value in another column
- GROUP BY a column in another MySQL table
- Update a table based on StudentId value in MySQL?
- Find the sum of a column values based on another numerical column in R.
- How to Apply Conditional Formatting to a Column Based on Another Column in Excel?
- How to Auto-Number a Column Based on the Cell Values on Another Column in Excel?
- Update one column data to another column in MySQL if the second column is NOT NULL?
- SUM a column based on a condition in MySQL
- How can we create a new MySQL table by selecting specific column/s from another existing table?
- How to assign a column value in a data frame based on another column in another R data frame?
- Best way to update a single column in a MySQL table?

Advertisements