MySQL - INSERT INTO from a Temporary Table

扶醉桌前 提交于 2019-12-06 11:03:27

You want insert . . . select:

insert into  metadata_values(elem_id,value, resource_type, resource_id, foreign_id,
                             mtvr_id, mett_id
                            ) 
    select 62, 'test', 4, insert_table.resource_id, insert_table.file_repos_id, 80, 4
    from insert_table;

Using insert_table.column_name does not automatically use the table. To do that youo need to either use a variable or a select statement.

I think what you are looking for is the following, though it will insert multiple rows with elem_id 62 if they exist in insert_table. In fact this will insert N rows with the same elem_id, value, mtvr_id, and met_id, where N is number of rows in insert_table.

insert     into 
    metadata_values (elem_id,value,resource_type,resource_id,foreign_id,mtvr_id,mett_id) 
values
    Select    
        62,'test',4,insert_table.resource_id,insert_table.file_repos_id,80,4
    From    insert_table

You can save yourself the memory overhead by skipping the temporary table altogether.

 insert     into 
    metadata_values (elem_id,value,resource_type,resource_id,foreign_id,mtvr_id,mett_id) 
values
     Select    
        62,mv.VALUE,4,r.resource_id,fr.file_repos_id,80,4
    From    resources r
    join  versions v on v.RESOURCE_ID = r.resource_id
    join  metadata_values mv on mv.resource_id = r.resource_id
    join  file_repository fr on fr.file_repos_id = v.foreign_id

 where  
         v.version_status = 'C'
     and r.RESOURCE_TYPE = 4
     and fr.file_title in ('suburbs')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!