Copy values of one column to another using INSERT and SELECT in a single MySQL query


Let us first create a table −

mysql> create table DemoTable1
(
   Name varchar(100),
   Gender ENUM('MALE','FEMALE')
);
Query OK, 0 rows affected (0.50 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1 values('Chris','Male');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1 values('Emma','Female');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable1;

This will produce the following output −

+-------+--------+
| Name  | Gender |
+-------+--------+
| Chris | MALE   |
| Emma  | FEMALE |
+-------+--------+
2 rows in set (0.00 sec)

Following is the query to create the second table −

mysql> create table DemoTable2
(
   EmployeeGender varchar(100)
);
Query OK, 0 rows affected (0.47 sec)

Let us now copy values of one column to another with a single MySQL query −

mysql> insert into DemoTable2(EmployeeGender) select Gender from DemoTable1;
Query OK, 2 rows affected (0.15 sec)
Records: 2 Duplicates: 0 Warnings: 0

Display all records from the table using select statement  −

mysql> select *from DemoTable;

This will produce the following output −

+----------------+
| EmployeeGender |
+----------------+
| MALE           |
| FEMALE         |
+----------------+
2 rows in set (0.00 sec)

Updated on: 25-Sep-2019

574 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements