Syntax error MySQL statement to copy data from one table and insert into another table

MySQL statement to copy data from one table and insert into another table



For this, you can use INSERT INTO….SELECT statement. Let us first create a table −

mysql> create table DemoTabe1 (Marks int);
Query OK, 0 rows affected (0.66 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTabe1 values(68);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTabe1 values(89);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTabe1 values(99);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTabe1 values(39);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTabe1 values(49);
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTabe1;

This will produce the following output −

+-------+
| Marks |
+-------+
| 68    |
| 89    |
| 99    |
| 39    |
| 49    |
+-------+
5 rows in set (0.00 sec)

Following is the query to create second table −

mysql> create table DemoTabe2 (Score int);
Query OK, 0 rows affected (0.66 sec)

Here is the query for inserting data to another table −

mysql> insert into DemoTabe2(Score) select Marks from DemoTabe1;
Query OK, 5 rows affected (0.16 sec)
Records: 5 Duplicates: 0 Warnings: 0

Display all records from the table using select statement −

mysql> select *from DemoTabe2;

This will produce the following output. Now the same records are visible in the second table −

+-------+
| Score |
+-------+
| 68    |
| 89    |
| 99    |
| 39    |
| 49    |
+-------+
5 rows in set (0.00 sec)
Updated on: 2019-08-23T06:42:48+05:30

470 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements