Computer >> Computer tutorials >  >> Programming >> MySQL

Copy from one column to another (different tables same database) in MySQL?


To copy from one column to another, you can use INSERT INTO SELECT statement.

Let us first create a table −

mysql> create table DemoTable1 (PlayerScore int);
Query OK, 0 rows affected (0.46 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1 values(98);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable1 values(81);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1 values(76);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1 values(88);
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable1;

This will produce the following output -

+-------------+
| PlayerScore |
+-------------+
| 98          |
| 81          |
| 76          |
| 88          |
+-------------+
4 rows in set (0.00 sec)

Here is the query to create a second table −

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

Here is the query to copy from one column to another (different tables same database) MySQL −

mysql> insert into DemoTable2(Marks) select PlayerScore from DemoTable1;
Query OK, 4 rows affected (0.19 sec)
Records: 4 Duplicates: 0 Warnings: 0

Display all records from the table using select statement −

mysql> select *from DemoTable2;

This will produce the following output -

+-------+
| Marks |
+-------+
| 98    |
| 81    |
| 76    |
| 88    |
+-------+
4 rows in set (0.00 sec)