【问题标题】:How to update a field in one table based off values in another table in oracle [duplicate]如何根据oracle中另一个表中的值更新一个表中的字段[重复]
【发布时间】:2015-02-20 15:31:08
【问题描述】:

在 Oracle 中,我试图根据第二个表中字段的计算来更新一个表中的字段,但我似乎无法使用任何语法。

例如,

Update item1 i1, item2 i2
Set i1.min_qty = i2.cases * i2.qty_per_case
Where i1.item_id = i2.item_id
And i1.flag1 = 'Y'

i1.item_idi2.item_id 之间的关系是一对一的。

任何帮助将不胜感激。

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    您需要使用相关的子查询或合并语句:

    update item1 i1
    set    i1.min_qty = (select i2.cases * i2.qty_per_case
                         from   item2 i2
                         where  i1.item_id = i2.item_id)
    where  i1.flag1 = 'Y';
    
    merge into item1 tgt
    using item2 src
      on (tgt.item_id = src.item_id)
    when matched then
    update set tgt.min_qty = src.cases * src.qty_per_case
    where  tgt.flag1 = 'Y';
    

    注意未经测试。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 2014-02-20
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多