SQL Server: join three tables

I was able to join 2 SQL tables use the following query:

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

Then I tried to join 3 SQL tables like below:

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?

Thanks!

Answers


If you use the proper ANSI JOIN syntax, you won't have any of those issues:

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 ,

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 . 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)

Need Your Help

poll system for Symfony2

php mysql symfony plugins walkthrough

I want to make a poll system into my website like http://dilemmaplugin.com/, this is what I want but is just for Wordpress...