假设我的数据库中有一张地铁地图,每条线路的每个站点都是一行。
如果我想知道我的线路连接在哪里:
mysql> SELECT LineA.stop_id FROM LineA, LineB WHERE LineA.stop_id = LineB.stop_id;
+---------+
| stop_id |
+---------+
| 1100 |
| 1300 |
+---------+
如何将此结果加入到stations表中以便检索stations名称?
桌子在这里:
stations
+----------------+---------+
| name | stop_id |
+----------------+---------+
| Nation | 1100 |
| Republic | 1300 |
| Main_st | 1065 |
| Airport | 1204 |
+----------------+---------+
LineA
+----------------+---------+
| id | stop_id |
+----------------+---------+
| 1 | 1100 |
| 2 | 1300 |
| 3 | 1204 |
+----------------+---------+
LineB
+----------------+---------+
| id | stop_id |
+----------------+---------+
| 1 | 1100 |
| 2 | 1300 |
| 3 | 1265 |
+----------------+---------+
最佳答案
你很接近你的答案:试试这个
select B.* from (SELECT LineA.stop_id FROM LineA, LineB WHERE LineA.stop_id = LineB.stop_id) as A
INNER JOIN stations as B on A.stop_id=B.stop_id