0

我要创建一个查询来计算列的值并将该值更新到表中。这个专栏是CAR_PRICE。目前,我创建的查询更新了所有值,但该列中的所有值都变得相同。但是,我的目标是计算每个特定行的值。

UPDATE CAR_PAYMENT_TBL
    SET CAR_PRICE =
    (SELECT 
        (R.END_DATE-R.START_DATE)*5 AS CAR_PRICE
        FROM CAR_TBL C
        LEFT JOIN
        ROOM_TBL R
        ON C.BOOKING_ID = R.BOOKING_ID
        WHERE C.TICKET_NO = '&TICKET_NO');

无论如何修改此查询以计算值并CAR_PRICE为 中的每一行更新不同的值CAR_PAYMENT_TBL

表格:

CREATE TABLE CAR_PAYMENT_TBL
(
    TICKET_NO INT NOT NULL PRIMARY KEY,
    CAR_PRICE NUMERIC(5,2) 
);

CREATE TABLE ROOM_TBL
(  
    STAY_NO INT NOT NULL PRIMARY KEY,
    ROOM_NO VARCHAR2(4) NOT NULL references ROOM_DETAILS_TBL(ROOM_NO),
    START_DATE DATE NOT NULL,
    END_DATE DATE NOT NULL,
    BOOKING_ID INT NOT NULL references BOOKING_TBL(BOOKING_ID)
);

CREATE TABLE CAR_TBL
(
    REG_ID VARCHAR2(7) NOT NULL PRIMARY KEY,
    TICKET_NO INT NOT NULL references CAR_PAYMENT_TBL(TICKET_NO),
    BOOKING_ID INT NOT NULL references BOOKING_TBL(BOOKING_ID)
);
4

1 回答 1

2
UPDATE CAR_PAYMENT_TBL T
    SET CAR_PRICE = (
        SELECT (R.END_DATE-R.START_DATE)*5 AS CAR_PRICE
        FROM CAR_TBL C
        LEFT JOIN ROOM_TBL R
          ON C.BOOKING_ID = R.BOOKING_ID
        WHERE C.TICKET_NO = T.TICKET_NO
    )
    WHERE T.TICKET_NO = '&TICKET_NO';
于 2013-03-21T20:29:54.783 回答