简体   繁体   English

如何在三个表上使用连接

[英]How to use join on three tables

I have three tables我有三张桌子

Table1 Table2 and Table3.表 1 表 2 和表 3。

Table1 having column ID.具有列 ID 的表 1。 Table2 having column names ID,Name.表 2 具有列名称 ID、名称。 Table three having column name Name.表三具有列名称 Name。

Now i want to retrive ID from table1 which is there in Table2 by so that the name associated with ID in table to should be in Table3.现在我想从 Table2 中的 table1 中检索 ID,以便与 table 中的 ID 关联的名称应该在 Table3 中。

Table1.ID=Table2.ID(Table2.Name=Table3.Namw). Table1.ID=Table2.ID(Table2.Name=Table3.Namw)。

Without using IN operator.Only joins.不使用 IN 运算符。仅连接。

select table1.id, table2.name
from table1
join table2 on table2.id = table1.id
join table3 on table3.name = table2.name
select distinct t1.ID
from Table1 t1
    ,Table2 t2
    ,Table3 t3
where t1.ID = t2.ID
  and t2.Name = t3.Name
;

or或者

select t1.ID
from Table1 t1
where exists (
  select 1
  from Table2 t2
      ,Table3 t3
  where t1.ID = t2.ID
    and t2.Name = t3.Name
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM