两个未链接的表之间的交互

Interaction between two tables that aren't linked

我有以下 3 个 tables:

CREATE TABLE Flight
(   
    ID_Flight number (10) not null,
    Status varchar (50) not null,
    Price varchar (10) not null,
    Boarding date,
    PRIMARY KEY (ID_Flight)
)

CREATE TABLE Stopover
(
    ID_Stopover number (10) not null,
    ID_Flight number,
    PRIMARY KEY (ID_Stopover),
    FOREIGN KEY (ID_Flight) REFERENCES Flight (ID_Flight)
)

CREATE TABLE Ticket
(
    ID_Ticket number (10),
    ID_Stopover number,
    Seat varchar (5) not null,
    Price varchar (10) not null,
    PRIMARY KEY (ID_Ticket),
    FOREIGN KEY (ID_Stopover) REFERENCES Stopover (ID_Stopover)
)

如您所见,tables Flight 和 Ticket 都有一个名为 "Price" 的列。请注意,航班和机票之间 link 的 table 是中途停留的。 ID_Stopover 是 Ticket 中的 FK,ID_Flight 是 Stopover 中的 FK。 我的目标是以某种方式从 Price (Flight) 列导入 Price (Ticket) 的值。 像这样:

ID_Flight -> 1 | Price (Flight) -> 0,99
ID_Flight -> 2 | Price (Flight) -> 0,00
ID_Flight -> 3 | Price (Flight) -> 00,00

ID_Ticket -> 1 | Price (Ticket) -> 0,00 (same value from ID_Flight 2)
ID_Ticket -> 2 | Price (Ticket) -> 0,00 (same value from ID_Flight 2)
ID_Ticket -> 7 | Price (Ticket) -> 0,00 (same value from ID_Flight 1)
select * 
from (
select f.id_flight, 
       t.id_ticket, 
       f.price flight_price, 
       t.price ticket_price
from flight f, ticket t, stopover s
where f.id_flight = s.id_flight 
and t.id_stopover = s.id_stopover
)
where id_flight = &x

您可以使用 merge 根据链接 table 的值更新您的 table:

merge into ticket t
using ( select *
        from stopOver 
          inner join flight 
            using(id_flight)
      ) sub
on (t.ID_Stopover = sub.ID_Stopover)
when matched then
  update set price = sub.price