Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 查询以联接两个表_Mysql_Join - Fatal编程技术网

Mysql 查询以联接两个表

Mysql 查询以联接两个表,mysql,join,Mysql,Join,我是MYSQL的新手。在此方面的任何帮助都将不胜感激 我有两个带有以下字段的表Airport和Post: 机场 id, Airport-code, Airport_name 帖子 id, Source_Airport_code, Destination_airport_code, Date_of_departure, preference 如何获取具有以下字段的记录(此处源和目的地对应于机场名称而不是代码): 尝试以下方法开始: select s.airport_name as sou

我是MYSQL的新手。在此方面的任何帮助都将不胜感激

我有两个带有以下字段的表Airport和Post:

机场

id, Airport-code, Airport_name
帖子

id, Source_Airport_code, Destination_airport_code, Date_of_departure, preference
如何获取具有以下字段的记录(此处源和目的地对应于机场名称而不是代码):

尝试以下方法开始:

select 
  s.airport_name as source, 
  d.airport_name as destination, 
  p.date_of_departure
from posts p
  inner join airports s
    on p.source_airport_code = s.id
  inner join airports d
    on d.source_airport_code = d.id

我不想这么说,但我认为你还有很长的路要走。

如果《代码》中的FK帖子。*(u Airport)代码并不像MJB假设的那样是指机场.id,而是指机场。机场代码

SELECT
  APS.Airport_name AS Source,
  APD.Airport_name AS Destination,
  Posts.date_of_departure
FROM Posts
  INNER JOIN Airports APS ON(APS.Airport_code = Posts.Source_Airport_code)
  INNER JOIN Airports APD ON(APD.Airport_code = Posts.Destination_airport_code)
哦,我想你是对的。我应该读得更仔细些。
SELECT
  APS.Airport_name AS Source,
  APD.Airport_name AS Destination,
  Posts.date_of_departure
FROM Posts
  INNER JOIN Airports APS ON(APS.Airport_code = Posts.Source_Airport_code)
  INNER JOIN Airports APD ON(APD.Airport_code = Posts.Destination_airport_code)