SQL使用where子句连接多个表

SQL使用where子句连接多个表,sql,sql-server,Sql,Sql Server,我想写一个sql语句: Select * FROM table1 inner join table2 AS t2 ON inner join table3 AS t3 ON inner join table4 AS t4 ON Where FK_Client_ID = 4 所有表都有相同的客户机ID。因此,不确定在上的上执行什么操作 在t2.FK_Client_ID=…上是否会出现类似的情况 因此,我只需要这些表中具有共同FK_Client_ID的所有数据。试试这个 Select

我想写一个sql语句:

Select * FROM table1
inner join table2 AS t2
  ON 
inner join table3 AS t3
  ON
inner join table4 AS t4
  ON
Where FK_Client_ID = 4
所有表都有相同的客户机ID。因此,不确定在上的
上执行什么操作

在t2.FK_Client_ID=…
上是否会出现类似的情况

因此,我只需要这些表中具有共同FK_Client_ID的所有数据。

试试这个

Select * FROM table1 t1
inner join table2 AS t2
  ON t2.FK_Client_ID = t1.FK_Client_ID
inner join table3 AS t3
  ON t3.FK_Client_ID = t1.FK_Client_ID
inner join table4 AS t4
  ON t4.FK_Client_ID = t1.FK_Client_ID
Where t1.FK_Client_ID = 4

如果您的外部字段名是
“FK\U客户端ID”
,而表1中的主键是
“客户端ID”


由于您使用的是内部联接,无论您使用
table1
还是其他任何一个进行联接,上述查询都会导致列名称“FK_Client_ID”不明确。您在table1中提到主键列为“Client_ID”,因此“where”条件应为table1。Client_ID或其他表的别名(t2、t3或t4).FK_客户端\u ID
Select * FROM table1 t1 inner join table2 t2
     ON t1.FK_Client_ID = t2.FK_Client_ID 
        inner join table3 t3
     ON t1.FK_Client_ID = t3.FK_Client_ID
         inner join table4 t4
     ON t1.FK_Client_ID = t4.FK_Client_ID
 Where t1.FK_Client_ID = 4
Select * FROM table1 t1 inner join table2 t2
     ON t1.FK_Client_ID = t2.FK_Client_ID 
        inner join table3 t3
     ON t1.FK_Client_ID = t3.FK_Client_ID
         inner join table4 t4
     ON t1.FK_Client_ID = t4.FK_Client_ID
 Where t1.FK_Client_ID = 4
Select *,
     (Select FK_Client_ID from table2 where FK_Client_ID = t1.FK_Client_ID  )As TID1,
     (Select FK_Client_ID from table3 where FK_Client_ID = t1.FK_Client_ID  )As TID2,
     (Select FK_Client_ID from table4 where FK_Client_ID = t1.FK_Client_ID  )As TID3 
FROM table1 t1