it-swarm.com.de

So verbinden Sie mehrere Spalten aus verschiedenen Tabellen

Wie können Sie mehrere Tabellen verbinden und Werte aus Tabellen anzeigen, auf die in anderen Tabellen weiter verwiesen wird?

select booking.booking_id,
(select customer.first_name from customer
where customer.customer_id=booking.customer_id) as First_Name,
(select customer.last_name from customer
where customer.customer_id=booking.customer_id) as Last_Name,
(select aircraft_model.aircraft_model from aircraft_model
where aircraft_model_id = (select aircraft.aircraft_model_id from 
aircraft
where aircraft_modle_id = booking.aircraft_id))

from booking;

Wie Sie sehen, versuche ich, den Wert von aircraft_model_id von der Tabelle booking zum aircraft_model Tabelle.

Die einzige Tabelle, die das aircraft_model Tabelle zur booking Tabelle ist die aircraft Tabelle ..

Ich bin völlig neu in SQL, also geben Sie bitte an, ob es Fehler gibt.

In den Codes für die Tabelle hinzugefügt:

CREATE TABLE Booking (
Booking_ID            NUMBER(10)   NOT NULL PRIMARY KEY,
Company_ID            NUMBER(10)   NOT NULL REFERENCES 
Penrhyn_Jet_Charter(Company_ID),
Customer_ID           NUMBER(10)   NOT NULL REFERENCES 
Customer(Customer_ID),
Aircraft_ID           NUMBER(10)   NOT NULL REFERENCES 
Aircraft(Aircraft_ID),
Assignment_No         NUMBER(10)   NOT NULL,
Booking_Date          DATE         DEFAULT SYSDATE,
Charter_Cost          NUMBER(14,2) CHECK(Charter_Cost > 0),
Departure_Date        DATE         NOT NULL,
Departure_Location    CHAR(3)      NOT NULL,
Arrival_Date          DATE         NOT NULL,
Arrival_Destination   CHAR(3)      NOT NULL
);

CREATE TABLE Aircraft_Model (
Aircraft_Model_ID     NUMBER(10)    NOT NULL PRIMARY KEY,
Aircraft_Model        VARCHAR2(255) NOT NULL,
No_Of_Aircrafts       NUMBER(10)    NOT NULL CHECK (No_Of_Aircrafts > 
0),
Aircraft_Description  VARCHAR2(500) 
);

CREATE TABLE Aircraft (
Aircraft_ID       NUMBER(10) NOT NULL PRIMARY KEY,
Company_ID        NUMBER(10) NOT NULL REFERENCES 
Penrhyn_jet_Charter(Company_ID),
Aircraft_Model_ID NUMBER(10) NOT NULL REFERENCES 
Aircraft_Model(Aircraft_Model_ID),
Seats             NUMBER(10) NOT NULL
);
1
domin thomas

Versuchen Sie dies, verwenden Sie INNER JOIN von booking bis aircraft_model

SELECT
    b.booking_id,
    c.firstname AS First_Name,
    c.last_name AS Last_Name,
    am.aircraft_model
FROM booking b
INNER JOIN customer c ON b.customer_id = c.customer_id
INNER JOIN aircraft a ON b.aircraft_id = a.aircraft_model_id
INNER JOIN aircraft_model am ON a.aircraft_model_id = am.aircraft_model_id
2

Die folgende Abfrage zeigt die gewünschte Ausgabe für die oben angegebene Frage an.

SELECT
booking.booking_id,
customer.first_name,
customer.last_name,
aircraft.aircraft_id,
aircraft_model.aircraft_model
FROM
booking
INNER JOIN customer ON customer.customer_id = booking.customer_id
INNER JOIN aircraft ON aircraft.aircraft_id = booking.aircraft_id
INNER JOIN aircraft_model ON aircraft_model.aircraft_model_id = 
aircraft.aircraft_model_id;
2
domin thomas