MySQL查询用另一个表中的文本替换ID号

发布于 2025-02-09 00:27:18 字数 2392 浏览 2 评论 0原文

我是第一次学习MySQL,到目前为止,我已经能够解决问题,但是我遇到了一个练习任务,我似乎无法缠绕我的头。

我在这里查看了多个类似的问题,但是我很难适应我自己的情况。

我有两个桌子:

Cities

+----+-----------+
| id | name      |
+----+-----------+
| 1  | Helsinki  |
+----+-----------+
| 2  | Stockholm |
+----+-----------+
| 3  | Oslo      |
+----+-----------+
| 4  | London    |
+----+-----------+

飞行

+----+---------+----------+
| id | from_id | to_id    |
+----+---------+----------+
| 1  | 1       | 2        |
+----+---------+----------+
| 2  | 1       | 3        |
+----+---------+----------+
| 3  | 2       | 3        |
+----+---------+----------+
| 4  | 2       | 4        |
+----+---------+----------+
| 5  | 1       | 2        |
+----+---------+----------+
| 6  | 2       | 1        |
+----+---------+----------+
| 7  | 1       | 3        |
+----+---------+----------+
| 8  | 3       | 1        |
+----+---------+----------+

我想创建一个具有这样最终结果的查询:

+-----------+-----------+
| FROM      | TO        |
+-----------+-----------+
| Helsinki  | Stockholm |
+-----------+-----------+
| Helsinki  | Oslo      |
+-----------+-----------+
| Stockholm | Oslo      |
+-----------+-----------+
| Stockholm | London    |
+-----------+-----------+
| Helsinki  | Stockholm |
+-----------+-----------+
| Stockholm | Helsinki  |
+-----------+-----------+
| Helsinki  | Oslo      |
+-----------+-----------+
| Oslo      | Helsinki  |
+-----------+-----------+

基本上我只想用城市名称替换ID号,但是我想要仅在查询中执行此操作,不想替换数据本身中的任何值。我该如何实现?

在这里您可以找到我的情况的测试案例: http://sqlfiddle.com/#!9/7475a2/7475a2/5/5/0

它应该提供创建和填充桌子所需的一切,也应该提供我尝试过的许多查询之一,但我没有成功。

链接到我查看过的一些类似主题:

,我也无法正确理解。

I'm learning MySQL for the first time, and I've been able to figure things out so far, but I've ran into a practice assignment that I just can't seem to wrap my head around.

I've viewed multiple similar issues here, but I've had trouble adapting them to my own situation.

I have two tables:

Cities

+----+-----------+
| id | name      |
+----+-----------+
| 1  | Helsinki  |
+----+-----------+
| 2  | Stockholm |
+----+-----------+
| 3  | Oslo      |
+----+-----------+
| 4  | London    |
+----+-----------+

Flights

+----+---------+----------+
| id | from_id | to_id    |
+----+---------+----------+
| 1  | 1       | 2        |
+----+---------+----------+
| 2  | 1       | 3        |
+----+---------+----------+
| 3  | 2       | 3        |
+----+---------+----------+
| 4  | 2       | 4        |
+----+---------+----------+
| 5  | 1       | 2        |
+----+---------+----------+
| 6  | 2       | 1        |
+----+---------+----------+
| 7  | 1       | 3        |
+----+---------+----------+
| 8  | 3       | 1        |
+----+---------+----------+

I want to create a query which has an end result like this:

+-----------+-----------+
| FROM      | TO        |
+-----------+-----------+
| Helsinki  | Stockholm |
+-----------+-----------+
| Helsinki  | Oslo      |
+-----------+-----------+
| Stockholm | Oslo      |
+-----------+-----------+
| Stockholm | London    |
+-----------+-----------+
| Helsinki  | Stockholm |
+-----------+-----------+
| Stockholm | Helsinki  |
+-----------+-----------+
| Helsinki  | Oslo      |
+-----------+-----------+
| Oslo      | Helsinki  |
+-----------+-----------+

Basically I just want to replace the id numbers with city names, but I want to do this only in the query, don't want to replace any values in the data itself. How do I achieve this?

Here you can find a test case for my situation:
http://sqlfiddle.com/#!9/7475a2/5/0

It should come with everything necessary for creating and populating the tables and also one of the many queries I've tried, but which I had no success with.

Links to some similar topics I've looked at:

Even after trying to adapt solutions from these topics, and reading up on "SELECT" and "JOIN", I can't get it right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一百个冬季 2025-02-16 00:27:18

Flights表加入Cities表,两次:

SELECT c1.name, c2.name
FROM Flights f
INNER JOIN Cities c1 ON c1.id = f.from_id
INNER JOIN Cities c2 ON c2.id = f.to_id
ORDER BY f.id;

Join the Flights table to the Cities table, twice:

SELECT c1.name, c2.name
FROM Flights f
INNER JOIN Cities c1 ON c1.id = f.from_id
INNER JOIN Cities c2 ON c2.id = f.to_id
ORDER BY f.id;
微暖i 2025-02-16 00:27:18
select fromCity.name , toCity.name  
from Flights
inner join Cities as fromCity on Flights.from_id = fromCity.id 
inner join Cities as toCity on Flights.to_id = toCity.id 
select fromCity.name , toCity.name  
from Flights
inner join Cities as fromCity on Flights.from_id = fromCity.id 
inner join Cities as toCity on Flights.to_id = toCity.id 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文