从查询结果中填充表格(mysql)

13

我想用现有表格的查询结果来填充一个表格。我该怎么做?

3个回答

22

(您不需要匹配表格模式)

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

参见INSERT...SELECT语法的文档。


可能需要使用 INSERT INTO tbl_name (col1, col2)...。 - Sam

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

目标表和源查询必须在列数和数据类型上匹配。
请参见此链接

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接