MySQL查询用另一个表中的文本替换ID号
我是第一次学习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:
- MySQL JOIN to replace IDs with value from another table
- SQL Replace multiple variables from another table in query result
- Replaces id with a value from another table
Even after trying to adapt solutions from these topics, and reading up on "SELECT" and "JOIN", I can't get it right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
Flights
表加入Cities
表,两次:Join the
Flights
table to theCities
table, twice: