
- 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
Copy column values from one table into another matching IDs in MySQL
Let us first create a table −
mysql> create table DemoTable1 ( PersonId int, Value int ); Query OK, 0 rows affected (0.64 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(100,78); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable1 values(101,67); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1 values(102,89); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+----------+-------+ | PersonId | Value | +----------+-------+ | 100 | 78 | | 101 | 67 | | 102 | 89 | +----------+-------+ 3 rows in set (0.00 sec)
Following is the query to create the second table.
mysql> create table DemoTable2 ( StudentId int, StudentScore int ); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2 values(100,NULL) ; Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable2 values(102,NULL); Query OK, 1 row affected (0.22 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2;
This will produce the following output −
+-----------+--------------+ | StudentId | StudentScore | +-----------+--------------+ | 100 | NULL | | 102 | NULL | +-----------+--------------+ 2 rows in set (0.00 sec)
Following is the query to copy column value from one table into another matching ids −
mysql> update DemoTable1, DemoTable2 set DemoTable2.StudentScore = DemoTable1.Value where DemoTable2.StudentId=DemoTable1.PersonId; Query OK, 2 rows affected (0.13 sec) Rows matched: 2 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable2;
This will produce the following output −
+-----------+--------------+ | StudentId | StudentScore | +-----------+--------------+ | 100 | 78 | | 102 | 89 | +-----------+--------------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL statement to copy data from one table and insert into another table
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?
- How to copy rows from one table to another in MySQL?
- How do I select data from one table only where column values from that table match the column values of another table in MySQL?
- Copy from one column to another (different tables same database) in MySQL?
- How to copy a table from one MySQL database to another?
- Simplest way to copy data from one table to another new table in MySQL?
- How to insert values from one table into another in PostgreSQL?
- MySQL query for INSERT INTO using values from another table?
- Copy values from one array to another in Numpy
- Easiest way to copy values of one column to a new table in MySQL?
- MySQL query to copy records from one table to another with different columns
- Copy values of one column to another using INSERT and SELECT in a single MySQL query
- Copy a few columns from a table to another in MySQL
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?

Advertisements