
- 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
Updating a MySQL table with values from another table?
We can update another table with the help of inner join. Let us create two tables.
Creating a table
mysql> CREATE table tblFirst -> ( -> id int, -> name varchar(100) -> ); Query OK, 0 rows affected (0.48 sec)
Inserting records
mysql> INSERT into tblFirst values(1,'John'); Query OK, 1 row affected (0.17 sec) mysql> INSERT into tblFirst values(2,'Bob'); Query OK, 1 row affected (0.26 sec) mysql> INSERT into tblFirst values(3,'David'); Query OK, 1 row affected (0.20 sec)
Displaying all records
mysql> SELECT * from tblFirst;
The following is the output
+------+-------+ | id | name | +------+-------+ | 1 | John | | 2 | Bob | | 3 | David | +------+-------+ 3 rows in set (0.00 sec)
Creating second table
mysql> CREATE table UpdTable -> ( -> IncId int auto_increment, -> primary key(IncId), -> id int, -> name varchar(100) -> ); Query OK, 0 rows affected (0.57 sec)
Inserting records
mysql> INSERT into UpdTable(id,name) values(1,'Taylor'); Query OK, 1 row affected (0.12 sec) mysql> INSERT into UpdTable(id,name) values(2,'jason'); Query OK, 1 row affected (0.24 sec) mysql> INSERT into UpdTable(id,name) values(3,'carol'); Query OK, 1 row affected (0.14 sec) mysql> INSERT into UpdTable(id,name) values(4,'john'); Query OK, 1 row affected (0.16 sec)
Displaying all records
mysql> SELECT * from UpdTable;
The following is the output
mysql> SELECT *from UpdTable; +-------+------+--------+ | IncId | id | name | +-------+------+--------+ | 1 | 1 | Taylor | | 2 | 2 | jason | | 3 | 3 | carol | | 4 | 4 | john | +-------+------+--------+ 4 rows in set (0.00 sec)
Look at the above output, the last name is matching from the first table record. Now, I will write the query for UPDATE −
mysql> UPDATE UpdTable -> inner join tblFirst ON (UpdTable.name = tblFirst.name) -> SET UpdTable.id = tblFirst.id; Query OK, 1 row affected (0.19 sec) Rows matched: 1 Changed: 1 Warnings: 0
We have updated the last record as follows −
The query is
mysql> SELECT * from UpdTable;
The following is the output
+-------+------+--------+ | IncId | id | name | +-------+------+--------+ | 1 | 1 | Taylor | | 2 | 2 | jason | | 3 | 3 | carol | | 4 | 1 | john | +-------+------+--------+ 4 rows in set (0.00 sec)
Look at the sample output. The id is updated, which was 4 but now it is 1.
- Related Articles
- Insert values in a table by MySQL SELECT from another table in MySQL?
- MySQL query for INSERT INTO using values from another table?
- How do I select data from one table only where column values from that table match the column values of another table in MySQL?
- Can we add a column to a table from another table in MySQL?
- Copy column values from one table into another matching IDs in MySQL
- How can we update the values in one MySQL table by using the values of another MySQL table?
- Create a table in MySQL that matches another table?
- Replace the empty values from a MySQL table with a specific value
- Updating a MySQL table row column by appending a value from user defined variable?
- MySQL query to insert data from another table merged with constants?
- Insert from one table with different structure to another in MySQL?
- Update data in one table from data in another table in MySQL?
- Select some data from a database table and insert into another table in the same database with MySQL
- Reshuffle the values in a table with MySQL
- Fetch specific rows from a MySQL table with duplicate column values (names)?

Advertisements