从查询结果填充表(mysql)

时间:2022-10-13 12:31:33

I would like to fill a table with the results of a query on existing table. How can I do that?

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

3 个解决方案

#1


18  

(You don't need to match the table schemas)

(您不需要匹配表模式)

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

See the reference for INSERT ... SELECT Syntax

请参阅INSERT ... SELECT语法的参考

#2


6  

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

The target table and the source query must match in number of columns and datatypes

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

See this link

看到这个链接

#3


4  

You can even create tables this way, though there the column names must match, or the select results are put in automatically added columns:

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

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)

#1


18  

(You don't need to match the table schemas)

(您不需要匹配表模式)

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

See the reference for INSERT ... SELECT Syntax

请参阅INSERT ... SELECT语法的参考

#2


6  

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

The target table and the source query must match in number of columns and datatypes

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

See this link

看到这个链接

#3


4  

You can even create tables this way, though there the column names must match, or the select results are put in automatically added columns:

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

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)