Populating a table from query results in MySQL?


To populate a table from query results, use the following syntax:

INSERT yourTableName(yourColumnName1,yourColumnName2,yourColumnName3,..........N)
SELECT yourColumnName1,yourColumnName2,yourColumnName3,..........N FROM yourAnotherTableName;

To understand the above syntax, let us create a table. The first table is as follows with some records. The query to create a table is as follows:

mysql> create table PopulateTableDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Name varchar(20),
   -> Amount int,
   -> ArrivalDateTime datetime,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.68 sec)

Now you can insert some records in the table using insert command. The query is as follows:

mysql> create table PopulateTableDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Name varchar(20),
   -> Amount int,
   -> ArrivalDateTime datetime,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.68 sec)

Insert some records in the table using insert command. The query is as follows:

mysql> insert into PopulateTableDemo(Name,Amount,ArrivalDateTime) values('John',456,'2018-02-4');
Query OK, 1 row affected (0.17 sec)
mysql> insert into PopulateTableDemo(Name,Amount,ArrivalDateTime)
values('Carol',1000,'2019-10-21');
Query OK, 1 row affected (0.17 sec)
mysql> insert into PopulateTableDemo(Name,Amount,ArrivalDateTime) values('Sam',970,'2019-07-25');
Query OK, 1 row affected (0.14 sec)
mysql> insert into PopulateTableDemo(Name,Amount,ArrivalDateTime) values('Larry',1050,'2015-10-28');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement. The query is as follows:

mysql> select *from PopulateTableDemo;

The following is the output:

+----+-------+--------+---------------------+
| Id | Name  | Amount | ArrivalDateTime     |
+----+-------+--------+---------------------+
|  1 | John  | 456    | 2018-02-04 00:00:00 |
|  2 | Carol | 1000   | 2019-10-21 00:00:00 |
|  3 | Sam   | 970    | 2019-07-25 00:00:00 |
|  4 | Larry | 1050   | 2015-10-28 00:00:00 |
+----+-------+--------+---------------------+
4 rows in set (0.00 sec)

Now you can create a second table and populate the value from the above table. The query to create the second table is as follows:

mysql> create table PopulateQueryFromAnotherTable
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> UserName varchar(20),
   -> Salary int,
   -> DepartureDateTime datetime,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (1.30 sec)

Here is the query to populate values from the first table which is PopulateTableDemo to PopulateQueryFromAnotherTable:

mysql> insert PopulateQueryFromAnotherTable(UserName,Salary,DepartureDateTime)
   -> select Name,Amount,ArrivalDateTime from PopulateTableDemo;
Query OK, 4 rows affected (0.15 sec)
Records: 4 Duplicates: 0 Warnings: 0

Now check the second table records using select statement. The query is as follows:

mysql> select *from PopulateQueryFromAnotherTable;

The following is the output:

+----+----------+--------+---------------------+
| Id | UserName | Salary | DepartureDateTime   |
+----+----------+--------+---------------------+
|  1 | John     | 456    | 2018-02-04 00:00:00 |
|  2 | Carol    | 1000   | 2019-10-21 00:00:00 |
|  3 | Sam      | 970    | 2019-07-25 00:00:00 |
|  4 | Larry    | 1050   | 2015-10-28 00:00:00 |
+----+----------+--------+---------------------+
4 rows in set (0.00 sec)

Updated on: 30-Jul-2019

394 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements