1

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.

0

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1
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

Comments

0

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.

3 Comments

Thank you, but the trouble is that i need 1 row from the first table and two rows from the second table.
A join like the one I'm describing would be able to achieve this. Didn't just want to write a quer for you as this sounds homeworky.
Don't worry, the project is much bigger than this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.