更新查询以更改特定行的值

时间:2013-03-21 20:02:43

标签: sql oracle sql-update row

我要创建一个查询来计算列的值并将此值更新到表中。此列为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

TABLES:

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)
);

1 个答案:

答案 0 :(得分: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';