我想用现有表格的查询结果来填充一个表格。我该怎么做?
(您不需要匹配表格模式)
INSERT tbl_name (col1, col2)
SELECT value1, value2
FROM othertable
参见INSERT...SELECT语法的文档。
您甚至可以通过这种方式创建表格,但是列名必须匹配,否则选择结果将被放置在自动添加的列中:
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)