Multi table sql query
I need to create a query with multiple tables for a project for school, but I'm not really good at this.
I've got 2 tables.
The first is a table with trajects:
departure_time (time)
arrival_time (time)
departure_id (int)
arrival_id (int)
The second table holds the names of the locations开发者_开发知识库:
location_id (int)
name (varchar)
I would need a query that gets the departure_time
, arrival_time
and the names of the departure place
and the arrival place
.
With an inner join;
select
departure_time,
arrival_time,
depart.name,
arrive.name
from trajects
inner join locations depart on (depart.location_id = trajects.departure_id)
inner join locations arrive on (arrive.location_id = trajects.arrival_id)
SELECT
t.departure_time,
t.arrival_time,
d.name as 'DeparturePlace',
a.name as 'ArrivalPlace'
FROM
Trajects t, Locations d, Locations a
WHERE
t.departure_id = d.location_id AND
t.arrival_id = a.location_id
What you'd need to do is an SQL JOIN on the departure_id and the arrival_id onto the second table. Details and examples here.
精彩评论