[英]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? 我正在建立一个机票网站,我有2个表,AIRPORTS和AIRPLANES,AIRPLANES表有2列,分别包含其机场目的地ID(ID_DESTINATION)和其机场起点ID(ID_ORIGIN),我想做的就是循环全部使用飞机
while
与AIRPORT_NAME作为其出发地和目的地的机场表,我该怎么办呢?
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? 为什么不只使用sql-join?
http://www.sql-join.com/ 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) 注意,我将O别名为O,以目的地D为别名。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.