How can we copy data with some condition/s from existing MySQL table?

MySQLMySQLi Database

As we know that we can copy the data and structure from an existing table by CTAS script. If we want to copy data with some condition/s then we need to use WHERE clause with CTAS script. Consider the example below −

mysql> Create table EMP_BACKUP2 AS SELECT * from EMPLOYEE WHERE id = 300 AND Name = 'Mohan';
Query OK, 1 row affected (0.14 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> Select * from EMP_BACKUP2;
| Id   | Name  |
| 300  | Mohan |

1 row in set (0.00 sec)

In the example above, we have created a table named EMP_BACKUP1 from table ‘Employee’ with some conditions. MySQL creates the table with only one row based on those conditions.

Updated on 29-Jan-2020 05:15:52