【问题标题】:Update the column using data from another two base tables使用来自另外两个基表的数据更新列
【发布时间】:2022-12-18 19:22:29
【问题描述】:
update tp 
set total_cost = (select h_package 
                  from hv, tp 
                  where hh_id = h_id) +
                 (select t_package 
                  from tourism, tp 
                  where tourism.t_id = tp.t_id);

【问题讨论】:

  • 问题是什么?并且请详细描述您正在尝试的内容以及您的预期结果与实际结果之间的差异。如果需要,包括您的表架构和示例数据(不要发布图像)。在 db<>fiddle 中设置一个最小示例并将链接发回此处会有所帮助。
  • Bad habits to kick : using old-style JOINs - 老式的逗号分隔的表格列表样式被替换为恰当的ANSI JOIN 语法在 ANSI-92SQL 标准 (30年!!之前)并且不鼓励使用它

标签: sql oracle


【解决方案1】:

由于您没有发布表格的描述,因此很难提出建议;如果您在列名前加上表的别名会有所帮助,但您只是部分这样做了。

看来您实际上是想将h_package 添加到t_package 并将结果放入total_cost。如果是这样,那么加入同一个select语句中的表,例如

update tp set
  tp.total_cost = (select h.h_package + t.t_package
                   from hv h join tourism t on t.h_id = h.h_id
                   where t.t_id = tp.t_id
                  );

它假定您想要影响tp 表中的所有行。如果没有,您将必须包含一个 where 子句,该子句将过滤掉您不想更新的行。

或者 - 可能更简单 - 使用merge

merge into tp 
using (select h.t_id,
              h.h_package + t.t.package as total_cost
       from hv h join tourism t on t.h_id = h.h_id
      ) x
         
on (tp.t_id = x.t_id)
when matched then update set
  tp.total_cost = x.total_cost;

再一次:我不知道我是否设法猜出了列名,但我发布的代码应该可以帮助您入门。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 2015-02-01
    相关资源
    最近更新 更多