[英]Joining multiple fields to one in another table
I have two tables:我有两个表:
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 |
I want do a WHERE tblItinerary = 1
to see the result set as:我想做一个
WHERE tblItinerary = 1
来查看结果集:
Place1 | Place2 | Place3 |
Austria | Germany | Austria |
Right now the direction I have is something like现在我的方向是这样的
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;
The idea is correct, but there are some mistakes: to join one table multiple times, you should use unique aliases for each table.这个想法是正确的,但有一些错误:多次加入一个表,你应该为每个表使用唯一的别名。 Also, you have not actually joined
tblItinerary
anywhere and your join syntax is incorrect (your attempt to join tblPlaces.PlaceName
instead of tblPlaces
).此外,您实际上
tblItinerary
在任何地方加入tblItinerary
并且您的加入语法不正确(您尝试加入tblPlaces.PlaceName
而不是tblPlaces
)。 Try the following:请尝试以下操作:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.