【问题标题】:insert data into a table with a subquery使用子查询将数据插入表中
【发布时间】:2020-03-26 04:11:13
【问题描述】:

我正在尝试将数据插入带有子查询的表中,但出现错误。我不确定我哪里出错了。请帮忙

INSERT INTO temp_table_merging (image_id,name,source,score)
SELECT table2.image_id,table2.name,table2.source,table2.score
FROM
    (
        SELECT a.image_id,a.name,a.source,a.score FROM master_table a
        WHERE a.image_id = temp_subset_table.image_id
    ) AS table2;

我得到的错误是

missing FROM-clause entry for table temp_subset_table

【问题讨论】:

    标签: mysql sql psql


    【解决方案1】:

    您的查询中需要一个 temp_subset_table 引用,例如:

    INSERT INTO temp_table_merging (image_id,name,source,score)
    SELECT  a.image_id,a.name,a.source,a.score 
    FROM master_table a
    JOIN temp_subset_table ON
      a.image_id = temp_subset_table.image_id
    

    删除了不需要的子查询。

    【讨论】:

      【解决方案2】:
      INSERT INTO temp_table_merging
      (image_id,name,source,score)
      SELECT table2.image_id
           , table2.name
           , table2.source
           , table2.score
        FROM
      (
          SELECT a.image_id
             , a.name
             , a.source
             , a.score 
          FROM master_table a
             , temp_subset_table
          WHERE a.image_id = temp_subset_table.image_id
      ) AS table2;
      

      你必须在内部选择加入 temp_subset_table

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-03
        • 2013-12-05
        • 1970-01-01
        • 1970-01-01
        • 2018-11-04
        • 1970-01-01
        • 1970-01-01
        • 2021-01-15
        相关资源
        最近更新 更多