MySQL innerjoin 3 tables

Suppose I had 3 tables

  • Passenger (Id [pk], name)
  • Airplane (registration [pk], num_seats)
  • Booking (Id, passenger_id [fk → Passenger ID], airplane_registration [fk → Airplane.registration])

The reservation table will be the connection point here, since there are many different relationships between the Passenger and Booking tables.

How can I select all the details related to a specific reservation identifier (for example, "abcde")?

Is it correct:

SELECT Passenger.name, Airplane.num_seats, Booking.Id FROM Booking JOIN Passenger ON Passenger.Id = Booking.passenger_Id JOIN Airplane ON Booking.airplane_registration = Airplane.registration WHERE Booking.Id = 'abcde'; 

Is this right to do? In addition, if I wanted to select all the orders and their details, would I do the same? (Without a where clause)?

I was looking to find out if this was correct for the test when MySQL went down to my machine.

+6
source share
2 answers

Yes, your request will work to get passenger and flight information for a specific booking ID. To receive all orders, I would add a SALE ORDER AND, if necessary, with the name of the passenger and registration for the flight.

+1
source

Without knowing your circuit, there is no way to be sure, but it looks very good to me.

0
source

Source: https://habr.com/ru/post/887668/


All Articles