繁体   English   中英

SQL oracle 如何将不同表中的两列相乘并将结果列放在两个表之一中

[英]SQL oracle How can i multiply two columns from different tables and put the resulting column in one of the two tables

我目前正在研究 SQL 项目,但我的代码无法正常工作,这是我尝试过的

UPDATE detail_order 
    SET total = (
        SELECT (detail_order.quantity*product.PRICE_EXCLVAT) 
        FROM detail_order,product 
        where detail_order.npro=product.npro
    );

但是 oracle 一直告诉我:ora-01427 单行子查询返回不止一行。 但我不知道如何解决这个问题,因为每个产品都有一个价格,每个订单只链接到一个产品,所以没有混淆。 有些产品被订购了好几次,但我不明白这怎么可能是个问题。

应该是这样的:

update detail_order d set
  d.total = (select d.quantity * p.price_exclvat
             from product p
             where p.npro = d.npro
            )
where exists (select null from product a
              where a.npro = d.npro
             );

或者

merge into detail_order d
  using product p
  on (p.npro = d.npro)
  when matched then update set d.total = d.quantity * p.price_exclvat;

这些都不起作用(即,如果product表中的nprodetail_ordernpro匹配的行不止一行,您将再次获得too_many_rows

那该怎么办? 这取决于你; 我们没有您的桌子,也不知道您的要求是什么。

我相信你想要的是这样的:

update detail_order d
  set total = quantity *
              (select price_exclvat from product where npro = d.npro)
;

quantity是您正在更新的同一行的数量。 没有理由引用detail_order来检索该的数量。

暂无
暂无

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

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