【发布时间】:2020-05-23 01:14:27
【问题描述】:
我有以下两张表:
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<th>airport</th>
</tr>
<tr>
<td>id_airport</td>
</tr>
<tr>
<td>name_airport</td>
</tr>
</table>
<h4>and</h4>
</body>
</html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<th>route</th>
</tr>
<tr>
<td>id_route</td>
</tr>
<tr>
<td>id_airport_origin</td>
</tr>
<tr>
<td>id_airport_destination</td>
</tr>
</table>
</body>
</html>
route.id_airport_origin 和 route.id_airport_destination 都是 airport.id_airport 的外键
我需要查询与其外键匹配的 name_airport 列。
我尝试执行以下查询,但没有成功。
SELECT route.id.route, airport.name_airport, airport.name_airport
FROM route
INNER JOIN airport ON route.id_airport_origin AND route.id_airport.destination = airport.id_airport
我已经尝试了上述代码的几种组合,但无法获得正确的查询。
有什么想法吗?
【问题讨论】:
-
这是一个常见问题解答。在考虑发布之前,请阅读您的教科书和/或手册和谷歌任何错误消息或您的问题/问题/目标的许多清晰、简洁和精确的措辞,有和没有您的特定字符串/名称和站点:stackoverflow.com 和标签;阅读许多答案。如果您发布问题,请使用一个短语作为标题。反映你的研究。 PS 对于 SQL 使用代码块,sn-ps 用于 css/html/javascript。
-
这是自联接。 How to get matching data from another SQL table for two different columns: Inner Join and/or Union? PS 请在代码问题中给出minimal reproducible example--cut & paste & runnable code,包括最小的代表性示例输入为代码;期望和实际输出(包括逐字错误消息);标签和版本;明确的规范和解释。对于包含 DBMS 和 DDL(包括约束和索引)和输入为格式化为表的代码的 SQL。 How to Ask
标签: sql join select foreign-keys inner-join