简体   繁体   English

将多个字段连接到另一个表中的一个

[英]Joining multiple fields to one in another table

I have two tables:我有两个表:

tblItinerary tbl行程

ID | Place1 | Time1 | Place2 | Time2 | Place3 | Time3 |
1  | 1      |   10  |   2    |   23  |    1   |   21  |
2  | 2      |   5   |   3    |   4   |    1   |   12  |

tblPlaces表位

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.

相关问题 SQL Server:将一个表中的行连接到另一个表中的列,并多次显示字段 - SQL Server : joining rows in one table to columns in another and displaying fields multiple times 在Sql中具有多个字段的联接表中,一个字段需要求和 - Joining table with multiple fields in Sql where one field needs to be summed 将一个表中的多个列连接到另一个表中的单个列 - Joining multiple columns in one table to a single column in another table 将多个值从一个表连接到另一个表的共享值 - Joining multiple values from one table to a shared value on another table MySQL的加入。 将多个行从一个表连接到另一个表? - MySQL Join. Joining multiple rows from one table to another? SQL 一个表中有 2 个字段的连接问题 - SQL Joining Issue with 2 fields in one table 将一个表中一个字段的值与另一表中多个字段的总和进行比较 - Comparing the value of a field in one table to the sum of multiple fields in another table 如何通过将一个表中的 id 与 bigquery 中另一表的多个列中的 id 连接来获取记录? - How to get records by joining id in one table with id in multiple columns in another table in bigquery? 如何通过将一个表中的 id 与 bigquery 中另一个表中的多个列中的 id 连接来获取记录以获取大表? - How to get records by joining id in one table with id in multiple columns in another table in bigquery for huge tables? 通过一个其他表将一个表连接到另一个表 - Joining one table through other tables to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM