我有两张桌子:
ID | Place1 | Time1 | Place2 | Time2 | Place3 | Time3 |
1 | 1 | 10 | 2 | 23 | 1 | 21 |
2 | 2 | 5 | 3 | 4 | 1 | 12 |
ID | PlaceName |
1 | Austria |
2 | Germany |
3 | India |
我想要WHERE tblItinerary = 1
来查看结果集:
Place1 | Place2 | Place3 |
Austria | Germany | Austria |
现在我的方向就像是
SELECT tblPlaces.PlaceName
FROM tblPlaces
JOIN tblPlaces.PlaceName
ON tblItinerary.Place1 = tblPlaces.PlaceName
JOIN tblPlaces.PlaceName
ON tblItinerary.Place2 = tblPlaces.PlaceName
JOIN tblPlaces.PlaceName
ON tblItinerary.Place3 = tblPlaces.PlaceName
WHERE tblItinerary.ID = 1;
答案 0 :(得分:0)
这个想法是正确的,但是有一些错误:要多次连接一个表,你应该为每个表使用唯一的别名。此外,您实际上并未在任何地方加入tblItinerary
,并且您的加入语法不正确(您尝试加入tblPlaces.PlaceName
而不是tblPlaces
)。请尝试以下方法:
select p1.PlaceName as Place1
, p2.PlaceName as Place2
, p3.PlaceName as Place3
from tblItinerary i
join tblPlaces p1 on i.Place1 = p1.ID
join tblPlaces p2 on i.Place2 = p2.ID
join tblPlaces p3 on i.Place3 = p3.ID
where i.ID = 1;