如何在不同列中搜索名字和姓氏的不同行中的多个名称?
How to search for multiple names on different rows where first name and last name on different columns?
ID
FIRST_NAME
LAST_NAME
1
SARA
JAMES
5
STEVE
STEPHEN
2
KAL
LEE
33
TONY
KAMPLOO
3
SANDRA
BROWN
23
HOPE
WHITE
FIRST_NAME
LAST_NAME
SARA
JAMES
我有 2 个 table:
表 1 有 ID、名字和姓氏,如上面的 table。
表 2 只有名字和姓氏。 (table1 的子集)。
我需要将 table2 中的名字与 table1 中的名字和 return 中的名字匹配???这只是一个示例。我的 table 很大,所以我需要一种有效的方法来做到这一点。
这看起来像普通的 INNER JOIN
:
select t1.*
from table1 t1
inner join table2 t2
on(t1.first_name = t2.first_name and t1.last_name = t2.last_name)
以下查询将显示第一个 table 中名字和姓氏出现在第二个 table 中的所有行。该操作称为 semi 连接。对于第一个 table 中的给定行,一旦在第二个 table 中找到匹配项,“加入”就会停止。 (在标准连接中,即使找到匹配项后搜索也会继续,因为连接必须找到 all 个匹配项——即使 we 知道有只有一个匹配项,Oracle 不知道所以它必须继续搜索。)
select t1.*
from t1
where (first_name, last_name) in (select first_name, last_name from t2);
ID | FIRST_NAME | LAST_NAME |
---|---|---|
1 | SARA | JAMES |
5 | STEVE | STEPHEN |
2 | KAL | LEE |
33 | TONY | KAMPLOO |
3 | SANDRA | BROWN |
23 | HOPE | WHITE |
FIRST_NAME | LAST_NAME |
---|---|
SARA | JAMES |
我有 2 个 table:
表 1 有 ID、名字和姓氏,如上面的 table。
表 2 只有名字和姓氏。 (table1 的子集)。
我需要将 table2 中的名字与 table1 中的名字和 return 中的名字匹配???这只是一个示例。我的 table 很大,所以我需要一种有效的方法来做到这一点。
这看起来像普通的 INNER JOIN
:
select t1.*
from table1 t1
inner join table2 t2
on(t1.first_name = t2.first_name and t1.last_name = t2.last_name)
以下查询将显示第一个 table 中名字和姓氏出现在第二个 table 中的所有行。该操作称为 semi 连接。对于第一个 table 中的给定行,一旦在第二个 table 中找到匹配项,“加入”就会停止。 (在标准连接中,即使找到匹配项后搜索也会继续,因为连接必须找到 all 个匹配项——即使 we 知道有只有一个匹配项,Oracle 不知道所以它必须继续搜索。)
select t1.*
from t1
where (first_name, last_name) in (select first_name, last_name from t2);