SQL Inner连接不允许在同一个表中

时间:2013-06-10 14:36:55

标签: sql sql-server sql-server-2008

在以下查询中,我尝试为rs1和rs2创建2个内部联接 但是在sql server中我只能选择rr1.ResourceId = rs2.Id 但我不能使rr1.ResourceId = rs1.Id

你能解释一下我应该怎么做吗?

select r.OwningTerritoryId ,rs1.ISRC , rs2.ISRC 
from Resource rs1 , Resource rs2
    inner join ReleaseResource rr1 
       on rr1.ResourceId=rs2.Id

感谢。

2 个答案:

答案 0 :(得分:1)

试试这样....

select r.OwningTerritoryId ,rs1.ISRC , rs2.ISRC from Resource rs1 
inner join ReleaseResource rr1 on rr1.ResourceId=rs2.Id

答案 1 :(得分:1)

您可以根据需要调整尽可能多的内连接...

select r.OwningTerritoryId ,rs1.ISRC , rs2.ISRC 
from Resource rs1
    inner join ReleaseResource rr2 
       on rr2.ResourceId=rs1.Id
    inner join ReleaseResource rr2
       on rr3.ResourceId=rs1.Id
    inner join ReleaseResource rr3
       on rr4.ResourceId=rs1.Id
    inner join ReleaseResource rr4
       on rr5.ResourceId=rs1.Id

虽然,[通常],您执行此操作的原因是每个连接需要不同的连接条件。

如果你想让连接在一对中的每个表之间,然后在这些对之间,那么试试:

select r.OwningTerritoryId ,rs1.ISRC , rs2.ISRC 
   from (Resource r1 join ReleaseResource rr1 
               On rr1.ResourceId = r1.Id)
      join (Resource r2 join ReleaseResource rr2 
               On rr2.ResourceId = r2.Id)
         On r2.id = r1.id   -- Here put condition for this join between the pairs