在 MySQL 中,CTAS(Create Table As Select)用于通过查询结果创建一个新表
CREATE TABLE new_table_name AS
SELECT column1, column2, ...
FROM existing_table_name
[WHERE condition];
这里是一些关键部分的解释:
new_table_name:要创建的新表的名称。column1, column2, ...:要从现有表中选择的列名称。如果需要所有列,可以使用 * 代替列名。existing_table_name:现有表的名称,即要从中复制数据的表。[WHERE condition]:可选的筛选条件,用于限制从现有表中选择的行。例如,假设我们有一个名为 employees 的现有表,包含以下列:id, first_name, last_name, salary 和 department。我们想要创建一个名为 high_salary_employees 的新表,其中包含薪水高于 50000 的员工。可以使用以下 CTAS 语句实现:
CREATE TABLE high_salary_employees AS
SELECT id, first_name, last_name, salary, department
FROM employees
WHERE salary > 50000;