简体   繁体   English

SQL Server:联接三个表

[英]SQL Server: join three tables

I was able to join 2 SQL tables use the following query: 我能够加入2个SQL表,使用以下查询:

SELECT * 
FROM Table1, Table2 with (nolock)
WHERE Table1.field1 = Table2.field2

Then I tried to join 3 SQL tables like below: 然后,我尝试加入3个SQL表,如下所示:

SELECT * 
FROM Table1, Table2, Table3 with (nolock)
WHERE Table1.field1 = Table2.field2, Table1.field2 = Table3.field3

But it didn't work. 但这没有用。 Did I miss anything here? 我在这里想念什么吗? Or how do I join 3 tables properly? 或如何正确加入3个桌子?

Thanks! 谢谢!

If you use the proper ANSI JOIN syntax, you won't have any of those issues: 如果使用正确的 ANSI JOIN语法,则不会出现以下任何问题:

SELECT * 
FROM 
    Table1
INNER JOIN 
    Table2 ON Table1.field1 = Table2.field2
INNER JOIN
    Table3 ON Table1.field2 = Table3.field3

You are joining table in old style and for multiple condition you have to use and instead of , 您在旧风格和多重条件加盟表格必须使用and替代,

Try to use inner join 尝试使用内部联接

Like this 像这样

SELECT * 
FROM Table1
inner join Table2 on  Table1.field1 = Table2.field2
inner join Table3 on Table1.field2 = Table3.field3

Try this : 尝试这个 :

SELECT * FROM Table1 WITH (NOLOCK)
inner join Table2 WITH (NOLOCK)
 on Table1.field1 = Table2.field2
inner join Table3 WITH (NOLOCK)
 on Table1.field2 = Table3.field3

Use AND instead of ',' in where condition . 在where条件中使用AND代替','。 Like this : 像这样 :

SELECT * 
FROM Table1, Table2, Table3 with (nolock)
WHERE Table1.field1 = Table2.field2 AND 
      Table1.field2 = Table3.field3

Try to use this one. 尝试使用这个。

   Select * from table1
            left join table2 on (table1.tablefield1 = table2.tablefield2)
            left join table3 on (table2.tablefield2 = table3.tablefield3)

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

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