简体   繁体   English

MySQL-从临时表插入

[英]MySQL - INSERT INTO from a Temporary Table

This seems stupidly easy but I'm stuck with a simple insert statement.See below: 这似乎很简单,但我坚持使用简单的插入语句。

begin work;
CREATE TEMPORARY TABLE IF NOT EXISTS insert_table AS 
(
select
     r.resource_id
    ,fr.file_repos_id
    ,mv.VALUE

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')
);


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

rollback work;

In the temp table line fr.file_title in ('suburbs') , the actual list is dynamically pulled from somewhere else (this is for demonstration purposes). fr.file_title in ('suburbs')的临时表行fr.file_title in ('suburbs') ,实际列表是从其他位置动态拉出的(出于演示目的)。 I get the following error message: 我收到以下错误消息:

Error Code: 1054. Unknown column 'insert_table.resource_id' in 'field list' 错误代码:1054。“字段列表”中的未知列“ insert_table.resource_id”

Now, I can run a select all over the temp table and it returns fine, its just failing at the update statement. 现在,我可以在整个temp表上运行一次select,它返回正常,只是在update语句中失败。 I'm running this from MySQL workbench. 我正在MySQL工作台上运行它。 Totally lost as to whats going on here. 完全不知道这里发生了什么。

You want insert . . . select 您要insert . . . select insert . . . select insert . . . select : 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. 使用insert_table.column_name不会自动使用该表。 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. 我认为您正在寻找的是以下内容,尽管如果insert_table中存在多行,则它将插入带有elem_id 62的多行。 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. 实际上,这将插入具有相同elem_id,value,mtvr_id和met_id的N行,其中N是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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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