Create a temporary table in a SELECT statement without a separate CREATE TABLE(在没有单独的 CREATE TABLE 的 SELECT 语句中创建临时表)
问题描述
是否可以在不使用 create table 语句并指定每个列类型的情况下从 select 语句创建临时(仅限会话)表?我知道派生表能够做到这一点,但那些是超临时的(仅限语句),我想重用.
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.
如果我不必编写创建表命令并保持列列表和类型列表匹配,这将节省时间.
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 语句中创建临时表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在没有单独的 CREATE TABLE 的 SELECT 语句中创建临时表


- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- SQL 临时表问题 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01