在没有单独的CREATE TABLE的SELECT语句中创建临时表 [英] Create a temporary table in a SELECT statement without a separate CREATE TABLE

查看:185
本文介绍了在没有单独的CREATE TABLE的SELECT语句中创建临时表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过select语句创建一个临时(仅会话)表,而无需使用create table语句并指定每种列类型?我知道派生表可以做到这一点,但是它们是超临时的(仅用于陈述),我想重用.

Is it possible to create a temporary (session only) table from a select statement without using a create table statement and specifying each column type? I know derived tables are capable of this, but those are super-temporary (statement-only) and I want to re-use.

如果我不必编写create table命令并保持列列表和类型列表匹配,则会节省时间.

It would save time if I did not have to write up a create table command and keep the column list and type list matched up.

推荐答案

CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS (SELECT * FROM table1)

http://dev.mysql上找到的手册中.com/doc/refman/5.7/en/create-table.html

创建表时,可以使用TEMPORARY关键字.临时表仅对当前会话可见,仅在当前会话中可见;在会话关闭时,该表将自动删除.这意味着两个不同的会话可以使用相同的临时表名称,而不会彼此冲突或与现有的具有相同名称的非TEMPORARY表冲突. (在删除临时表之前,现有表一直处于隐藏状态.)要创建临时表,您必须具有CREATE TEMPORARY TABLES特权.

You can use the TEMPORARY keyword when creating a table. A TEMPORARY table is visible only to the current session, and is dropped automatically when the session is closed. This means that two different sessions can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.) To create temporary tables, you must have the CREATE TEMPORARY TABLES privilege.

这篇关于在没有单独的CREATE TABLE的SELECT语句中创建临时表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆