2010-11-14 115 views
1

假設我有一個這樣的命令:從SELECT命令創建一個新表

select name, age, haircolor from people where shoesize > 10; 

有沒有辦法,我可以創建基於什麼命令返回一個新表的方法嗎?我想將該命令的輸出傳送到一個新表中。

含義:

mysql> show tables; 
+-----------------+ 
| Tables_in_db1 | 
+-----------------+ 
| people   | 
+-----------------+ 
1 rows in set (0.00 sec) 

mysql> CREATE TABLE FROM "select name, age, haircolor from people where shoesize > 10;"; 
..? 

mysql> show tables; 
+-----------------+ 
| Tables_in_db1 | 
+-----------------+ 
| people   | 
| NEW TABLE  | 
+-----------------+ 
1 rows in set (0.00 sec) 

回答

3

是的,CREATE TABLE語法支持在最後一個選項的SELECT語句,也可以是這樣簡單:

CREATE TABLE foo 
SELECT bar FROM xyz; 
1

是的,當然,例如:

CREATE TABLE your_table AS SELECT name, age -- ... 

你可以看到更多的有關語法here