As we all know, we can copy data and structures from existing tables through CTAS scripts. If we want to copy data with certain conditions then we need to use WHERE clause with CTAS script. Consider the following example -
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 above example, we have created a table named EMP_BACKUP1 from the table "Employee" based on some conditions. MySQL creates a table with only one row based on these conditions.
The above is the detailed content of How can we copy data with certain conditions from existing MySQL table?. For more information, please follow other related articles on the PHP Chinese website!