SQL乘以2列2个不同的表

时间:2017-10-05 02:54:34

标签: sql oracle join

我想要做的是计算diner_payment_due(DINER TABLE),将food_serve_cost(FOOD_SERVE TABLE)乘以food_serve_size(FS_DINER TABLE

CREATE TABLE diner (
diner_no            NUMBER(8) NOT NULL,
diner_payment_due   NUMBER(6,2) NOT NULL,
diner_seat_no       NUMBER(2) NOT NULL,
diner_seated        DATE NOT NULL,
diner_completed     DATE,
table_no            NUMBER(2) NOT NULL
);


CREATE TABLE food_serve (
food_item_no           NUMBER(4) NOT NULL,   
food_serve_size        CHAR(2) NOT NULL,
food_serve_kilojoules  NUMBER(4) NOT NULL,
food_serve_cost        NUMBER(5,2) NOT NULL
);


 CREATE TABLE FS_DINER (
 DINER_NO NUMBER(8) NOT NULL,
 FOOD_ITEM_NO NUMBER(4) NOT NULL,
 FOOD_SERVE_SIZE CHAR(2 BYTE) NOT NULL,
 FS_DINER_NO_SERVES NUMBER(1) NOT NULL,
 FS_DINER_ITEM_SERVED CHAR (1) NOT NULL,
 CONSTRAINT pk_fs_diner PRIMARY KEY (DINER_NO, FOOD_ITEM_NO,FOOD_SERVE_SIZE)
 );

我只需要返回单个值,然后插入到diner表中,我认为我需要Inner Join,然后只需sum (food_serve_cost * NO_SERVES)然后group by DINER_NO

INSERT INTO DINER ( DINER_NO, DINER_PAYMENT_DUE, DINER_SEAT_NO, DINER_SEATED, DINER_COMPLETED, TABLE_NO) INSERT INTO DINER VALUES (1, SELECT SUM (FOOD_SERVE.FOOD_SERVE_COST * FS_DINER..., , 1, 1/05/2017, 1 /05/2017, 1);

1 个答案:

答案 0 :(得分:0)

根据其他两个表中的值更新diner表。

INSERT INTO DINER 
  (SELECT fs.diner_no, 
          (fs.food_serve_cost * fd.FOOD_SERVE_SIZE),
          11,sysdate,sysdate,11 /*Am assuming, Due to Not null constraint, there will be error*/
     FROM food_serve fs,
           FS_DINER fd
     WHERE fs.food_item_no = fd.food_item_no)