从查询结果填充表(mysql)

时间:2009-03-10 21:24:34

标签: mysql

我想用现有表格上的查询结果填充表格。我怎么能这样做?

3 个答案:

答案 0 :(得分:18)

(您不需要匹配表模式)

INSERT tbl_name (col1, col2)
    SELECT value1, value2
    FROM othertable

请参阅INSERT ... SELECT Syntax

的参考资料

答案 1 :(得分:6)

insert into table_name ...
select * from table_name where ....

目标表和源查询必须匹配列数和数据类型

请参阅此link

答案 2 :(得分:4)

您甚至可以通过这种方式创建表,但列名必须匹配,或者将选择结果放入自动添加的列中:

mysql> create table foo ( id int primary key auto_increment, bar datetime )
    -> select now() as bar, now() as baz from dual;
Query OK, 1 row affected, 1 warning (0.06 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from foo;
+----+---------------------+---------------------+
| id | bar                 | baz                 |
+----+---------------------+---------------------+
|  1 | 2009-03-10 17:01:35 | 2009-03-10 17:01:35 |
+----+---------------------+---------------------+
1 row in set (0.00 sec)