简体   繁体   English

是否可以将一个表连接到另一个表,而第一个表中的一个列与第二个表中的多个列之一匹配?

[英]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 . 我有一个包含两个表PeopleItinerary的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. 在人员表中,列Item1Item2Item3匹配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: 您可以通过使用你想要做什么orinon子句:

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.

相关问题 连接两个表,检查第一个表中的一条记录是否与第二个表中的多条记录匹配 - Join two tables, check if one record in the first table matches with multiple records in the second 更好的方法来选择第一个表中的所有列,并在内连接上从第二个表中选择一列 - Better way to select all columns from first table and only one column from second table on inner join 如何将一个表中的两个或更多列与另一个表中的一个列联接在一起,即使第一个表列中的值为NULL - How to join two or more columns from one table with one column from another table, even there are NULL values in first table columns 汇总一个表,其中列与另一张表匹配,并将结果联接 - Sum one table where columns match another table and join the results SQL查询联接一列的平均值,其中另一列在另一表的两列值之间 - Sql query join average of one column where another column is between two columns values of another table Select 基于两个不同 where 子句/连接的表中的列 - 如果一个列匹配,则使用该列,如果不匹配则使用另一个 - Select columns from a table based on two different where clauses / joins - if one column matches use that, if not then use another 将一个表中的两列连接到另一引用表中的列 - Join two columns in one table to a column in another reference table 如何将SQL表中的2列连接到另一个SQL表中的一列? - How to join 2 columns in a SQL table to one column in another SQL table? 将一个表的两列连接到另一表的同一列 - Join two columns of one table to the same column of another table 将一列联接到另一张表 - Join in one column to another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM