Answer the question
In order to leave comments, you need to log in
How to make a selection with two foreign keys of one table to another table?
Let's say we have the following two tables:
CREATE TABLE Station
(
idStation INT NOT NULL AUTO_INCREMENT,
nameStation CHAR(50) NOT NULL,
PRIMARY KEY (idStation)
);
CREATE TABLE Ticket
(
idTrip INT NOT NULL,
idPassanger INT NOT NULL,
departureStation INT NOT NULL,
arrivalStation INT NOT NULL,
wagon INT NOT NULL,
place INT NOT NULL,
PRIMARY KEY (idTrip, idPassanger),
FOREIGN KEY (idTrip) REFERENCES Trip(idTrip),
FOREIGN KEY (idPassanger) REFERENCES Passanger(idPassanger),
FOREIGN KEY (departureStation) REFERENCES Station(idStation),
FOREIGN KEY (arrivalStation) REFERENCES Station(idStation)
);
+-----------+------------+--------------------+------------------+------------+-------+-------+-------+-------------------+-----------------+--------+-------+
| nameTrain | date | firstname | lastname | birthday | wagon | place | price | departureStation | arrivalStation | dep | arr |
+-----------+------------+--------------------+------------------+------------+-------+-------+-------+-------------------+-----------------+--------+-------+
SELECT Train.nameTrain, Trip.time as date,
Passanger.firstname, Passanger.lastname, Passanger.birthday,
Price.wagon, Ticket.place, Price.price,
Ticket.departureStation, Ticket.arrivalStation,
Station.nameStation as dep, Station.nameStation as arr
FROM Ticket
JOIN Trip
ON Ticket.idTrip = Trip.idTrip
JOIN Passanger
ON Ticket.idPassanger = Passanger.idPassanger
JOIN Train
ON Trip.idTrain = Train.idTrain
JOIN Price
ON Train.idTrain = Price.idTrain AND Ticket.wagon = Price.wagon
JOIN Station dst
ON Ticket.departureStation = dst.idStation AND dep = dst.nameStation
JOIN Station ast
ON Ticket.arrivalStation = ast.idStation AND arr = ast.nameStation;
ERROR 1054 (42S22): Unknown column 'Station.nameStation' in 'field list'
SELECT Train.nameTrain, Trip.time as date,
Passanger.firstname, Passanger.lastname, Passanger.birthday,
Price.wagon, Ticket.place, Price.price,
Ticket.departureStation, Ticket.arrivalStation,
Station.nameStation as dep, Station.nameStation as arr
FROM Ticket
JOIN Trip
ON Ticket.idTrip = Trip.idTrip
JOIN Passanger
ON Ticket.idPassanger = Passanger.idPassanger
JOIN Train
ON Trip.idTrain = Train.idTrain
JOIN Price
ON Train.idTrain = Price.idTrain AND Ticket.wagon = Price.wagon
JOIN Station
ON Ticket.departureStation = Station.idStation;
+-----------+------------+--------------------+------------------+------------+-------+-------+-------+------------------+----------------+-----------------------------------+-----------------------------------+
| nameTrain | date | firstname | lastname | birthday | wagon | place | price | departureStation | arrivalStation | dep | arr |
+-----------+------------+--------------------+------------------+------------+-------+-------+-------+------------------+----------------+-----------------------------------+-----------------------------------+
| 688Х | 2016-03-16 | Алик | Тимофеев | 1959-09-03 | 6 | 15 | 2107 | 1492 | 2097 | ЗЕЛЕНОГРАДСКАЯ | ЗЕЛЕНОГРАДСКАЯ |
|...
+-----------+------------+--------------------+------------------+------------+-------+-------+-------+------------------+----------------+-----------------------------------+-----------------------------------+
Answer the question
In order to leave comments, you need to log in
SELECT Train.nameTrain, Trip.time as date,
Passanger.firstname, Passanger.lastname, Passanger.birthday,
Price.wagon, Ticket.place, Price.price,
Ticket.departureStation, Ticket.arrivalStation,
dst.nameStation as dep, ast.nameStation as arr
FROM Ticket
JOIN Trip
ON Ticket.idTrip = Trip.idTrip
JOIN Passanger
ON Ticket.idPassanger = Passanger.idPassanger
JOIN Train
ON Trip.idTrain = Train.idTrain
JOIN Price
ON Train.idTrain = Price.idTrain AND Ticket.wagon = Price.wagon
JOIN Station dst
ON Ticket.departureStation = dst.idStation
JOIN Station ast
ON Ticket.arrivalStation = ast.idStation
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question