这些是我的表
Stop
id name
a place1
b place2
c place3
d place4
e place5
f place6
g place7
Trip
id start finish
1 a c
3 d f
4 e g
5 b f
目前我正在使用以下sql查询:
SELECT trip.id, starts, ends
FROM trip,
(SELECT name as starts
FROM trip, stop
WHERE stop.id=trip.start),
(SELECT name as ends
FROM trip, stop
WHERE stop.id=trip.finish);
这给了我这个样子
id starts ends
1 place1 place7
2 place1 place7
3 place1 place7
4 place1 place7
5 place1 place7
1 place1 place6
and so on...
我希望显示Trip表的副本,其中stop id(a,b,c ..)替换为其指定的名称。
id start finish
1 place1 place3
3 place4 place6
4 place5 place7
5 place2 place6
停止ID和Trip ID是PK, Trip表中的Start和Finish列是Stop表中Stop id的外键。我在oracle developer ver 3.2上工作。 你能帮忙吗?
答案 0 :(得分:2)
你在找这个吗?
SELECT
T.id,
S1.name Start,
S2.name End
FROM Trip T
JOIN Stop S1
T.Start = S1.Id
JOIN Stop S2
T.End = S2.Id