在 SELECT 语句中创建临时表,无需单独的 CREATE TABLE

发布于 2024-11-04 06:12:37 字数 141 浏览 1 评论 0原文

是否可以从 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心凉怎暖 2024-11-11 06:12:37
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 权限。

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

From the manual found at http://dev.mysql.com/doc/refman/5.7/en/create-table.html

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.

后eg是否自 2024-11-11 06:12:37

除了psparrow的答案之外,如果您需要添加索引到临时表,请执行以下操作:

CREATE TEMPORARY TABLE IF NOT EXISTS 
  temp_table ( INDEX(col_2) ) 
ENGINE=MyISAM 
AS (
  SELECT col_1, coll_2, coll_3
  FROM mytable
)

它也适用于PRIMARY KEY

In addition to psparrow's answer if you need to add an index to your temporary table do:

CREATE TEMPORARY TABLE IF NOT EXISTS 
  temp_table ( INDEX(col_2) ) 
ENGINE=MyISAM 
AS (
  SELECT col_1, coll_2, coll_3
  FROM mytable
)

It also works with PRIMARY KEY

千纸鹤 2024-11-11 06:12:37

使用这个语法:

CREATE TEMPORARY TABLE t1 (select * from t2);

Use this syntax:

CREATE TEMPORARY TABLE t1 (select * from t2);
尘曦 2024-11-11 06:12:37

发动机必须在选择之前:

CREATE TEMPORARY TABLE temp1 ENGINE=MEMORY 
as (select * from table1)

Engine must be before select:

CREATE TEMPORARY TABLE temp1 ENGINE=MEMORY 
as (select * from table1)
百思不得你姐 2024-11-11 06:12:37

当表包含 BLOB/TEXT 列时,不支持 ENGINE=MEMORY

ENGINE=MEMORY is not supported when table contains BLOB/TEXT columns

饮惑 2024-11-11 06:12:37

据我了解,如果您在 phpMyAdmin 等中使用 SELECT 语句,则该语句将在临时表上起作用,但在该 SELECT 之后,临时表将消失。这意味着首先准确设置您想要对其执行的操作,并且在更改数据(删除、更新)的“操作”语句完成之前不要查看任何结果。

As I understand it, a SELECT statement will work on the temporary table if you're using it in something like phpMyAdmin, but following that SELECT, the temporary table will be gone. This means set up exactly what you want to do with it first, and don't view any results till your 'action' statements that change the data (DELETE, UPDATE) are complete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文