如何将列添加到现有表但如何联接三个表

时间:2020-11-07 18:26:08

标签: sql oracle oracle-sqldeveloper

你好,我有一个数据库

我正在尝试在现有的“ SALE”表中添加一列,该表计算通过sale_price *(gallery_sale_percent / 100)计算的图库所需付款,以及通过sale_price *(1-0.2-( gallery_sale_percent / 100))

我尝试过

Alter table sale add sale_gallery_commission number(9,2);
comment on column sale.sale_gallery_commission is 'Commission payment to Gallery';
update sale 
set sale_gallery_commission = ( select sale_price * gallery_sale_percent from 
                     sale s JOIN aw_display  d ON s.aw_display_id = d.aw_display_id 
                     JOIN gallery     g ON g.gallery_id = d.gallery_id );

但是我收到一条错误消息说ORA-01427:单行子查询返回了多行

我该怎么过来?

1 个答案:

答案 0 :(得分:1)

我认为您需要一个相关的子查询,而不是join

update sale s
    set sale_gallery_commission =
        (select sale_price * gallery_sale_percent
         from aw_display d join
              gallery g 
              on g.gallery_id = d.gallery_id
         where s.aw_display_id = d.aw_display_id 
        );

也就是说,从您的问题尚不清楚这是否还会返回多行。如果是这样,那么您可能想要聚合:

update sale s
    set sale_gallery_commission =
        (select sum(sale_price * gallery_sale_percent)
         from aw_display d join
              gallery g 
              on g.gallery_id = d.gallery_id
         where s.aw_display_id = d.aw_display_id 
        );