[英]Is it possible to join one table to another, where one column in the first table matches one of multiple columns in the second?
I have a SQL database with two tables, People
and Itinerary
. 我有一个包含两个表People
和Itinerary
的SQL数据库。
People
table columns: People
表列:
Item1
Item2
Item3
Itinerary
table columns: Itinerary
表栏:
ID
COLUMN2
In the people table, the columns Item1
, Item2
, and Item3
match values in the ID
column for some row in the Itinerary
table. 在人员表中,列Item1
, Item2
和Item3
匹配ID
列中Itinerary
表中某行的值。
Is it possible to join the itinerary table to all three Item_
columns? 是否可以将行程表加入所有三个Item_
列? Eg, something like the following: 例如,如下所示:
SELECT DISTINCT Itinerary.COLUMN2
FROM People
JOIN Itinerary ON People.ITEM1 = Itinerary.ID
JOIN Itinerary ON People.ITEM2 = Itinerary.ID
JOIN Itinerary ON People.ITEM3 = Itinerary.ID
Note that the above query doesn't work. 请注意,以上查询无效。
You can do what you want by using or
or in
in the on
clause: 您可以通过使用你想要做什么or
或in
在on
子句:
SELECT DISTINCT ITINERARY.COLUMN2
FROM People pl JOIN
Itinerary i
ON i.ID IN (pl.ITEM1, pl.ITEM2, pl.ITEM3)
I'm not sure I understand your question entirely, but maybe something like this? 我不确定我是否完全理解您的问题,但是也许是这样?
SELECT DISTINCT Itinerary.COLUMN2
FROM People
LEFT OUTER JOIN Itinerary ON
(
Itinerary.ID = People.ITEM1
OR Itinerary.ID = People.ITEM2
OR Itinerary.ID = People.ITEM3
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.