SELECT INTO
选择任何查询所定义的行,并将这些行插入到新表中。您可以指定是创建临时表还是永久表。
语法
[ WITH with_subquery [, ...] ] SELECT [ TOP number ] [ ALL | DISTINCT ] * | expression [ AS output_name ] [, ...] INTO [ TEMPORARY | TEMP ] [ TABLE ] new_table [ FROM table_reference [, ...] ] [ WHERE condition ] [ GROUP BY expression [, ...] ] [ HAVING condition [, ...] ] [ { UNION | INTERSECT | { EXCEPT | MINUS } } [ ALL ] query ] [ ORDER BY expression [ ASC | DESC ] [ LIMIT { number | ALL } ] [ OFFSET start ]
有关此命令的参数的详细信息,请参阅SELECT。
示例
选择 EVENT 表中的所有行并创建 NEWEVENT 表:
select * into newevent from event;
选择聚合查询的结果并将这些结果插入到名为 PROFITS 的临时表中:
select username, lastname, sum(pricepaid-commission) as profit into temp table profits from sales, users where sales.sellerid=users.userid group by 1, 2 order by 3 desc;