Create a new table from a select command
Suppose I have a command like this:
select name, age, haircolor from people where shoesize > 10;
Is there a way that I can create a new table based on what that command returns? I want to pipe the output from th开发者_高级运维at command into a new table.
Meaning:
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)
Yes, the CREATE TABLE syntax supports an option SELECT statement at the end, it can be as simple as this:
CREATE TABLE foo
SELECT bar FROM xyz;
You can use MySQL's create table...select syntax:
You can create one table from another by adding a SELECT statement at the end of the CREATE TABLE statement:
CREATE TABLE new_tbl SELECT * FROM orig_tbl;
Yes, of course, for example :
CREATE TABLE your_table AS SELECT name, age -- ...
You can see more about the syntax here.
you can either create a table from that select statement
but then the data will be just copied
if you want the data to be the same and your new table be updated with the source and vice versa, you can create a view
精彩评论