O
O
oxaoo2016-02-17 23:33:09
MySQL
oxaoo, 2016-02-17 23:33:09

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

I want to make a selection with the output of the names of the departure and arrival stations, something like this:
+-----------+------------+--------------------+------------------+------------+-------+-------+-------+-------------------+-----------------+--------+-------+
| nameTrain | date       | firstname          | lastname         | birthday   | wagon | place | price | departureStation  | arrivalStation  | dep    | arr   |
+-----------+------------+--------------------+------------------+------------+-------+-------+-------+-------------------+-----------------+--------+-------+

By executing the following query:
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;

I am getting an error:
ERROR 1054 (42S22): Unknown column 'Station.nameStation' in 'field list'

And this next query:
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;

Gives the wrong result:
+-----------+------------+--------------------+------------------+------------+-------+-------+-------+------------------+----------------+-----------------------------------+-----------------------------------+
| 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

2 answer(s)
N
nozzy, 2016-02-18
@oxaoo

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

I
Igor, 2016-02-17
@unitby

You assigned aliases to tables. and use them.
replace
with
and AND dep = dst.nameStation and AND arr = ast.nameStation are redundant

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question