用另一个 table 的值更新 table

Update table with values of another table

我正在尝试通过子查询更新 table,但出现以下错误:

ORA-01427: 单行子查询 returns 多行 01427.00000 - "single-row subquery returns more than one row"

我想在列中插入第三个table的内容。

我的查询:

UPDATE GH212_TABLE1
SET GH212_LINK = (SELECT GH201_TABLE1.GH201_ATTRIBUTEX
                  FROM GH210_TABLE3
                  LEFT JOIN GH201_TABLE1
                  ON GH210_TABLE3.GH210_GH201_ID = GH201_TABLE1.GH201_ID
                  INNER JOIN GH212_TABLE1
                  ON GH212_TABLE1.GH212_GH210_ID = GH210_ID
                  WHERE GH212_TABLE1.GH212_GH210_ID=GH210_TABLE3.GH210_ID  
                  GROUP BY GH201_ATTRIBUTEX)

WHERE GH212_TABLE1.GH212_ATTRIBUTEY='11';

我不确定如何 link table 正确,以便从一个特定对象获取属性。

如有任何帮助,我们将不胜感激。

干杯, 法比

编辑:感谢您的回复!我对多行问题很满意,但我无法解决它。即使我尝试了不同的查询。但是 William Robertson 的解决方案似乎有效,非常感谢!!

SELECT GH201_TABLE1.GH201_ATTRIBUTEX
                  FROM GH210_TABLE3
                  LEFT JOIN GH201_TABLE1
                  ON GH210_TABLE3.GH210_GH201_ID = GH201_TABLE1.GH201_ID
                  INNER JOIN GH212_TABLE1
                  ON GH212_TABLE1.GH212_GH210_ID = GH210_ID
                  WHERE GH212_TABLE1.GH212_GH210_ID=GH210_TABLE3.GH210_ID  
                  GROUP BY GH201_ATTRIBUTEX

此查询可能会为您提供多个行值,因此您会收到此异常

ORA-01427: single-row subquery returns more than one row 01427. 00000 - "single-row subquery returns more than one row"

确保您的子查询 return 单行值。

有点猜测,因为我没有您的数据或业务逻辑,但也许您想要更像下面的内容。我已经从子查询中删除了 gh212_table1 的第二个实例,而是将 t3 链接回正在更新的 table:

update gh212_table1 t
set    gh212_link =
       ( select distinct t1.gh201_attributex
         from   gh210_table3 t3
                left join gh201_table1 t1 on t1.gh201_id = t3.gh210_gh201_id
         where  t3.gh210_id = t.gh212_gh210_id )
where  gh212_table1.gh212_attributey = '11';

不过,如果 gh212_table1 中的某些行子查询找不到任何行,那么这些行 gh212_link 将被设置为空。如果这是一个问题,您可以尝试将其重写为 merge:

merge into gh212_table1 tgt
using ( select distinct t3.gh210_id, t1.gh201_attributex
        from   gh210_table3 t3
               left join gh201_table1 t1 on t1.gh201_id = t3.gh210_gh201_id ) src
on    ( src.gh210_id = tgt.gh212_gh210_id )
when  matched then update
      set tgt.gh212_link = src.gh201_attributex;

当您从外连接中获取 gh201_attributex 时,只要 gh201_table1 中没有 gh210_table3 行的行,它就会为空。那是你要的吗?在 merge 版本中,如果将其设为内部联接,则合并将仅应用于存在行的位置。