SQL查询问题,如何制作一个输出两个表的所有条目的查询
我有两张桌子,城市
和航班
:
城市
ID | 名称 |
---|---|
1 | 纽约 |
2 | 帕里斯 |
3 | 东京 |
4 | 阿姆斯特丹 |
飞行
ID | Deckite_id | arrival_id |
---|---|---|
1 | 1 | 2 2 |
2 | 1 | 3 |
3 | 3 2 | 3 |
4 | 2 | 4 |
我需要编写一个查询,以找到所有飞行连接。
所需的输出将是:
Deckit_City | Arrival_city |
---|---|
纽约巴黎纽约 | 东京 |
巴黎 | 东京 |
巴黎 | 阿姆斯特丹 |
如何 | ? |
编写此查询
I have two tables, CITIES
and FLIGHTS
:
CITIES
id | name |
---|---|
1 | New York |
2 | Paris |
3 | Tokyo |
4 | Amsterdam |
FLIGHTS
id | departure_id | arrival_id |
---|---|---|
1 | 1 | 2 |
2 | 1 | 3 |
3 | 2 | 3 |
4 | 2 | 4 |
I need to write a query that finds all the flight connections.
The desired output would be:
departure_city | arrival_city |
---|---|
New York | Paris |
New York | Tokyo |
Paris | Tokyo |
Paris | Amsterdam |
How to write this query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用加入。内在的加入和左联盟都将达到您的目的:
与左JOIN:
OUTUPT:
Query with Inner Join:
output:
< em> db&lt;&gt; fiddle
You can use join for that. Both inner join and left join will serve your purpose:
Query with left join:
Outupt:
Query with inner join:
Output:
db<>fiddle here
您可以进行两次加入:
没有更多信息,目前尚不清楚您是否要进行左联接或内部连接,是否需要一个条款,是否需要订单。也许最好学习一些SQL基础知识,然后如果需要,请提出一个更精确的问题。
尝试如果需要:
You can do two joins:
Without further information, it's unclear if you want to do a left or inner join, if you need a where clause, if you need an order by etc. Maybe it would be better to learn some SQL basics and then ask a more precise question if necessary.
Try out if you want: db<>fiddle
另外,您可以使用以下SQL:
准备好的语句如下:
Also you can use below SQL:
The prepared statement is as below: