内部连接由外键链接的三个表

时间:2014-09-02 11:40:43

标签: sql sql-server tsql

我是SQL编程的新手,有以下场景。 我有三个表彼此链接为 -

ForceInstance

ForceInstanceID (PK)
ForceID (FK to Force)
Description
CreatedBy
CreatedOn

强制

ForceID (PK)
ForceTypeID (FK to ForceType)
Description
CreatedBy
CreatedOn`

ForceType指令

ForceTypeID (PK)
Description

我想连接三个表,我尝试了这个查询 -

Select * from ForceInstance FI inner join 
              Force F inner join
              ForceType FT
          on  FI.ForceID = F.ForceID
          and F.ForceTypeID = FT.ForceTypeID

但无法实现任何目标。我是SQL的新手,想知道如何实现这个目标。

3 个答案:

答案 0 :(得分:1)

Select * from ForceInstance FI 
inner join Force F on  FI.ForceID = F.ForceID
inner join ForceType FT on F.ForceTypeID = FT.ForceTypeID

答案 1 :(得分:1)

SELECT    * 
FROM      ForceInstance FI 
          INNER JOIN Force F 
              ON FI.ForceID = F.ForceID 
          INNER JOIN ForceType FT
              ON F.ForceTypeID = FT.ForceTypeID

答案 2 :(得分:0)

 Select * from ForceInstance FI  join 
              Force F 
              on  FI.ForceID = F.ForceID
             JOIN ForceType FT           
               on F.ForceTypeID = FT.ForceTypeID