简体   繁体   English

PHP MYSQL。 如何基于另一个表中的2个不同的ID从一个表中检索相同的数据

[英]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.

相关问题 如何根据复选框在不同的表中插入相同的数据? mysql php - how to insert same data in different table based on checkbox ? mysql php 在PHP中插入后如何从MYSQL表中检索ID - How to retrieve ID from MYSQL table after Insert in PHP 如何在一行中获取具有相同 id 但在另一列中具有不同值的数据并在 php 中的表中显示一个月 - How to fetch data with same id in a row but different value in another column and display one month into table in php 如何根据同一SELECT中不同的id从表中获取两个值 - How to get two values from table based on different id's in same SELECT 如何在php/mysql中根据id/column显示表中的数据 - How to display a data in the table based on id/column in php/mysql 如何从mysql php检索数据并在下拉列表中显示 - How to retrieve data from mysql php and show in table from dropdown PHP从一个表中获取ID,并从另一表中提取与这些ID相对应的数据 - PHP take id's from one table and pull data that corresponds with those id's from another table PHP MYSQL 从表到另一页显示相同的数据 - PHP MYSQL Displaying the same data from table to another page 使用PHP从MySQL表中检索特定数据 - Retrieve specific data from MySQL table with PHP 如何从php的mysql的每个表的特定行中检索数据? - how to retrieve data from particular rows of every table of mysql in php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM