Easiest way to copy values of one column to a new table in MySQL?

MySQLi DatabaseMySQL

For this, use AS select statement. Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable1 values(89);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1 values(98);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1 values(91);
Query OK, 1 row affected (0.24 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable1;

This will produce the following output −

+-------+
| Score |
+-------+
|    89 |
|    98 |
|    91 |
+-------+
3 rows in set (0.00 sec)

Following is the query to copy values of one column to a new table −

mysql> create table DemoTable2(
   StudentScore int
)AS select Score AS StudentScore from DemoTable1;
Query OK, 3 rows affected (0.81 sec)
Records: 3 Duplicates: 0 Warnings: 0

Let us check the table records once again −

mysql> select StudentScore from DemoTable2;

This will produce the following output −

+--------------+
| StudentScore |
+--------------+
|           89 |
|           98 |
|           91 |
+--------------+
3 rows in set (0.00 sec)
raja
Published on 01-Oct-2019 07:23:00
Advertisements