Display result of a table into a temp table with MySQL?


Let us first create a table −

mysql> create table DemoTable1 (Id int,Name varchar(100));
Query OK, 0 rows affected (0.89 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1 values(100,'John');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable1 values(110,'Chris');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1 values(120,'Robert');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1 values(130,'David');
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 | John   |
|  110 | Chris  |
|  120 | Robert |
|  130 | David  |
+------+--------+
4 rows in set (0.00 sec)

Following is the query to display result of a table into a temporary table −

mysql> create temporary table DemoTable2
   select *from DemoTable1;
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0

Let us check the table records of temporary table −

mysql> select *from DemoTable2;

This will produce the following output −

+------+--------+
| Id   | Name   |
+------+--------+
|  100 | John   |
|  110 | Chris  |
|  120 | Robert |
|  130 | David  |
+------+--------+
4 rows in set (0.00 sec)

Updated on: 23-Aug-2019

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements