[英]How to create temporary table with given column names from select?
How to create temporary table with given column names from select? 如何使用select中的给定列名创建临时表?
I mean something like this (I put "as newData" just to illustrate the idea, this doesn't work): 我的意思是这样的(我将“ as newData”作为示例来说明,这是行不通的):
CREATE TEMPORARY TABLE IF NOT EXISTS tmp AS
SELECT id, min(data) as newData FROM myTable WHERE id > 100 GROUP BY id;
So I can get a table like: 所以我可以得到一个像这样的表:
+-------------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | | 0 | |
| newData | int(10) unsigned | YES | | NULL | |
+-------------------+------------------+------+-----+---------+-------+
The problem is that if I create the table directly, the second field name contains forbidden symbols and I don't know how to use it: 问题是,如果我直接创建表,则第二个字段名称包含禁止的符号,并且我不知道如何使用它:
+-------------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | | 0 | |
| min(data) | int(10) unsigned | YES | | NULL | |
+-------------------+------------------+------+-----+---------+-------+
as newData
works just as you'd expect it to; as newData
可以按您期望的那样工作;
mysql> CREATE TABLE myTable (
id INT NOT NULL DEFAULT 0,
data INT
);
mysql> CREATE TEMPORARY TABLE tmp AS
SELECT id, min(data) as newData FROM myTable WHERE id > 100 GROUP BY id;
mysql> desc tmp;
+---------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| id | int(11) | NO | | 0 | |
| newData | int(11) | YES | | NULL | |
+---------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.