0

I'm building a website for Airplane Tickets, i have 2 tables, AIRPORTS and AIRPLANES, the AIRPLANES table has 2 columns containing its airport destination id(ID_DESTINATION) and its airport origin id(ID_ORIGIN), what i want to do is to loop all of the AIRPLANES using while with the AIRPORT_NAME as its origin and destination from the AIRPORTS table, how can i do that?

this is the query i'm using :

SELECT a.ID_PLANE, a.PLANE_NAME, 
       a.ID_ORIGIN, a.ID_DESTINATION, 
       a.TAKEOFF, a.LANDING, a.PRICE,
       b.ID_AIRPORT, b.AIRPORT_NAME
FROM AIRPLANES AS a
LEFT JOIN AIRPORTS AS b
ON a.ID_ORIGIN = b.ID_AIRPORT

Thanks in advance.

5
  • 2
    Welcome to StackOverflow! We will be glad to help you if you get stuck on a specific programming problem, but we are not here to write code for you. Please see How do I ask a good question? and What topics can I ask about here?. Commented Jun 6, 2017 at 19:42
  • Provide some sample data and expected results. It appears you may just need two joins to airports with different aliases. Once for groin, once for destination. SQL operates best in terms of "SETS" operating one at a time in loops is sub optimal for database systems. Commented Jun 6, 2017 at 19:49
  • @AlexHowansky I'm sorry, but i'm not asking anyone to write codes for me, I'm just asking "How do i retrieve the same data in a specific table based on 2 different ids from another table" , should i use join? union? transaction? cz i've tried them all, and its still not working Commented Jun 6, 2017 at 19:50
  • @xQbert so i have to use more than 1 query ? Commented Jun 6, 2017 at 19:51
  • No. Just an additional LEFT join to airports (example below) Commented Jun 6, 2017 at 20:01

2 Answers 2

1

Notice I aliased O for origin D for destination Assuming you just want the names of airports for each airplane (flight)

SELECT a.ID_PLANE
     , a.PLANE_NAME
     , a.ID_ORIGIN
     , a.ID_DESTINATION
     , a.TAKEOFF
     , a.LANDING
     , a.PRICE
     , o.ID_AIRPORT as Orig_ID
     , o.AIRPORT_NAME Orig_Name
     , d.ID_AIRPORT as Dest_ID
     , d.AIRPORT_NAME Dest_Name
FROM AIRPLANES AS a
LEFT JOIN AIRPORTS AS o
  ON a.ID_ORIGIN = b.ID_AIRPORT
LEFT JOIN AIRPORTS d
  ON a.ID_DESTINATION = b.ID_AIRPORT
Sign up to request clarification or add additional context in comments.

Comments

1

Why dont you just use a sql-join?
http://www.sql-join.com/

Comments

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.