简体   繁体   中英

PHP MYSQL. How to retrieve the same data from a table based on 2 different ID's in another table

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM