Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL join help needed 2

Status
Not open for further replies.

dephjam

Programmer
Mar 20, 2007
1
GR
hi there,
I have an airport table like this:

************
airports
------------
air_id
air_code
air_name
***********

and a flight table:

************
flight
------------
fl_id
fl_code
fl_depart_airport_id
fl_arrive_airport_id
***********

i need the select statement to get names for departure and arrivel airports. something like this:

***********************
fl_code
departure_airport_name
arrival_airport_name
***********************
any help much appreciated
 
Code:
SELECT fl_code,
  (select depart.airport_name
   FROM airports depart
   WHERE depart.fl_id = flights.fl_id) As DepartureAirport,

  (select arrive.airport_name
   FROM airports arrive
   WHERE arrive.fl_id = flights.fl_id) As ArrivalAirport,
FROM flights

John
 
If the solution above, with sub-queries, turns out to be too slow, try this instead:
[tt]
SELECT fl_code,
depart.air_name,
arrive.air_name
FROM flight,
airports AS depart,
airports AS arrive
WHERE depart.air_id = fl_depart_airport_id
AND arrive.air_id = fl_arrive_airport_id
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top