如何使用SQL Select Insert将一个表中的行复制到另一个表中

时间:2021-12-01 04:41:25

Hey guys am having a nightmare trying to figure out how to copy all the rows from a select query and insert to another table.

嘿,伙计们,我有一个噩梦,想弄明白如何从select查询中复制所有的行,然后插入到另一个表中。

如何使用SQL Select Insert将一个表中的行复制到另一个表中

My query returns this result after running this query SELECT wp_rg_lead.id, wp_rg_lead.created_by, wp_usermeta.meta_key, wp_usermeta.meta_value, wp_usermeta.user_id from wp_rg_lead inner join wp_usermeta on wp_rg_lead.id = wp_usermeta.user_id where wp_usermeta.meta_key = 'parents_id' and wp_rg_lead.id = 12; 如何使用SQL Select Insert将一个表中的行复制到另一个表中

我的查询在运行这个查询SELECT wp_rg_lead之后返回这个结果。id,wp_rg_lead。created_by wp_usermeta。meta_key wp_usermeta。meta_value wp_usermeta。user_id来自wp_rg_lead的内部连接wp_usermeta上的wp_rg_lead。id = wp_usermeta。user_id wp_usermeta的地方。meta_key = 'parents_id'和wp_rg_lead。id = 12;

2 个解决方案

#1


1  

If you want to select into an existing table use the following format:

如果您想要选择一个现有的表,请使用以下格式:

INSERT INTO destination_table
(column1,column2, etc...)
SELECT wp_rg_lead.id, 
           wp_rg_lead.created_by, 
           wp_usermeta.meta_key,
           wp_usermeta.meta_value, 
           wp_usermeta.user_id 
    FROM wp_rg_lead 
    inner join wp_usermeta on wp_rg_lead.id = wp_usermeta.user_id 
    WHERE wp_usermeta.meta_key = 'parents_id' and wp_rg_lead.id = 12;

Alternatively if you want to select them into a brand new table you can use this formate to create the table then insert the values:

或者,如果您想将它们选择到一个全新的表中,您可以使用该formate创建表,然后插入值:

SELECT wp_rg_lead.id, 
               wp_rg_lead.created_by, 
               wp_usermeta.meta_key,
               wp_usermeta.meta_value, 
               wp_usermeta.user_id
        INTO destination_table 
        FROM wp_rg_lead 
        inner join wp_usermeta on wp_rg_lead.id = wp_usermeta.user_id 
        WHERE wp_usermeta.meta_key = 'parents_id' and wp_rg_lead.id = 12;

In either query you need to change the destination_table to the new table name. In the first query you need to supply column names as well.

在这两个查询中,都需要将destination_table更改为新的表名。在第一个查询中,您还需要提供列名。

#2


1  

INSERT INTO yourtable (yourcolumns) select * from yourothertable

#1


1  

If you want to select into an existing table use the following format:

如果您想要选择一个现有的表,请使用以下格式:

INSERT INTO destination_table
(column1,column2, etc...)
SELECT wp_rg_lead.id, 
           wp_rg_lead.created_by, 
           wp_usermeta.meta_key,
           wp_usermeta.meta_value, 
           wp_usermeta.user_id 
    FROM wp_rg_lead 
    inner join wp_usermeta on wp_rg_lead.id = wp_usermeta.user_id 
    WHERE wp_usermeta.meta_key = 'parents_id' and wp_rg_lead.id = 12;

Alternatively if you want to select them into a brand new table you can use this formate to create the table then insert the values:

或者,如果您想将它们选择到一个全新的表中,您可以使用该formate创建表,然后插入值:

SELECT wp_rg_lead.id, 
               wp_rg_lead.created_by, 
               wp_usermeta.meta_key,
               wp_usermeta.meta_value, 
               wp_usermeta.user_id
        INTO destination_table 
        FROM wp_rg_lead 
        inner join wp_usermeta on wp_rg_lead.id = wp_usermeta.user_id 
        WHERE wp_usermeta.meta_key = 'parents_id' and wp_rg_lead.id = 12;

In either query you need to change the destination_table to the new table name. In the first query you need to supply column names as well.

在这两个查询中,都需要将destination_table更改为新的表名。在第一个查询中,您还需要提供列名。

#2


1  

INSERT INTO yourtable (yourcolumns) select * from yourothertable