查询联接两个表 [英] Query to join two tables

查看:105
本文介绍了查询联接两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MYSQL的新手.任何帮助,将不胜感激.

I am a newbie in MYSQL. Any help on this will be appreciated.

我有两个表Airports和Posts,其中包含以下字段:

I have two tables Airports and Posts with the following Fields:

机场

id, Airport-code, Airport_name

帖子

id, Source_Airport_code, Destination_airport_code, Date_of_departure, preference

如何获取包含以下字段的记录(此处的源"和目的地"对应于机场名称而不是代码):

How do I get a record with the following fields (Here Source and Destination corresponds to Airport names instead of the codes):

Source, destination, date_of_departure

推荐答案

如果Posts.*_Airport_code中的FK不是MJB假定的Airports.id,而是Airports.Airport_code,则

If the FK in Posts.*_Airport_code doesn't refer to Airports.id as MJB assumes but to Airports.Airport_code then

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)

这篇关于查询联接两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆