How can we use WHERE condition when creating a table with CTAS (Create Table as Selected) script?

MySQLMySQLi Database

As we know that we can copy the data and structure from an existing table by CTAS script. Use of WHERE clause is demonstrated in 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.

raja
Published on 19-Feb-2018 10:33:04
Advertisements