简体   繁体   中英

How can i use SQL Select Insert to copy rows from one table to another

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.

desc wp_rg_lead_detail

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; 查询结果

INSERT INTO yourtable (yourcolumns) select * from yourothertable

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM