I have 3 tables that I am trying to pull information from. Sample data shown below
TABLE Trip
idTrip Title Date
1 Ben Lomond 08-08-2016
TABLE Person_has_trip
Trip_idTrip Person_idPerson
1 1
1 2
TABLE Person
idPerson Forename Surname
1 David Jack
2 Colin McAlpine
I am trying to get the name of each trip and the name of each person that has been on that trip. Separately the queries look like this
1. SELECT idTrip, title, Date from Trip
2. SELECT Person_idPerson from Person_has_Trip where Trip_idTrip = $idTrip
3. SELECT forename, surname from person where idPerson = $idPerson
Is there a way I can combine these 3 queries? I thought I had a working solution, but discovered today that it was missing some data.
The query I thought was working but isn't working is below
select trip.idTrip, trip.title, trip.date, Person_has_Trip.Person_idPerson, person.forename, person.surname
from trip
inner join
Person_has_Trip
on trip.idTrip = Person_has_Trip.trip_idTrip
inner join
person
on Person_has_Trip.Person_idPerson = person.idPerson
ORDER BY trip.date
Any suggestions would be excellent. I am currently learning SQL as I go, so some of the more advanced features like joins are, at the moment, a little over my head.
When I say it isn't working, it isn't display all the data I expect. It displays some, but not all.
xQbert got it.
The Person_has_trip table was missing data so the query was then missing trips as it wasn't listed properly in Person_has_trip.
Thank you for all your help. As soon as people started saying that my query looked good and that it should work I had to have been something wrong with the data.
Excellent, thank you once again! :)